Python 3.x 学习:Python 基础代码(二)

时间:2021-12-28 03:06:53
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Robin HU的python的代码,以下是基本的数据类型操作。
#python 中含有5种数据类型:
#数字,字符串,列表,元组,字典。
#数字可分:int 带符号整型,float 浮点型,complex 复数。
#string 字符串可用‘a’,“a”,“”a“”,“”“a”“”,取元素用C语言中的[index]操作,可用+连接2个字符串,[-1]=!(字符串结束符)
#list 列表,用[]括起,中间用“,”分开:['aaa',1,-1],引用[index],可用+连接2个列表,[-1]表示最后一个元素
#tuple 元组用()括起,中间用“,”分隔,其元素不可写,类似只读列表,操作用[],元组可用+连接,[-1]表示最后一个元素
#字典用{}括起,中间用“,”分隔,使用[]分配和访问值,字典是一种哈希表,由键值对组成,字典键可以是python任何数据类型/对象
#list 成员函数:加到最后append(item),insert(index,item),删除末尾一个pop(),或pop(index)删除固定位置
# list可以含有List,

firstNum =4;
negnum = -2;
complex = 1+2j;
string = 'jj';
floataa = 1.0;
String = '01234567'; # string can be grouped by ' or ""
site = "www.google.com"; # string can be grouped by ' or ""
list = ['string',1,1.0,-3,3+5j];
###################basic variables########
print(firstNum);
print(negnum);
print(string);
print(floataa);
print(complex*complex);
print(String);
print(site);
print('String[0]:',String[0]);
print('String[1:]:',String[1:]);
print('String[-1]:',String[-1]);
###################list####################
print('list=',list);
print('list[0]=',list[0]);
print('list[1:]=',list[1:]);
print('list[-1]:',list[-1]);
print ('list[-3:-1] = ',list[-3:-1]);
print ('list[-1:1] = ',list[-1:1]);
print ('list[-1:0] = ',list[-1:0]);

tuple = ( 'maxsu', 786 , 2.23, 'yiibai', 70.2 )
tinytuple = (999.0, 'maxsu')

# tuple[1] = 'new item value' 不能这样赋值

print ('tuple = ', tuple) # Prints complete tuple
print ('tuple[0] = ', tuple[0]) # Prints first element of the tuple
print ('tuple[1:3] = ', tuple[1:3]) # Prints elements starting from 2nd till 3rd
print ('tuple[-3:-1] = ', tuple[-3:-1]) # 输出结果是什么?
print ('tuple[2:] = ', tuple[2:]) # Prints elements starting from 3rd element
print ('tinytuple * 2 = ',tinytuple * 2) # Prints tuple two times
print ('tuple + tinytuple = ', tuple + tinytuple) # Prints concatenated tuple

print('=================first test==============');
s1,s2=72,85;
rate = (s2-s1)/s1;
print('%.1f' % rate);
L = [
['you', 'Google', 'Microsoft'],
['Java', 'are', 'Ruby', 'PHP'],
['Adam', 'Bart', 'beautiful!']
]
print(L[0][0]);
print(L[1][1]);
print(L[-1][-1]);
print('=================second test==============');
##############以下是python条件分支代码#############
age = input('please inut your age: ');
if (((int(age))<18)&((int(age))>10)):
print('you are teenager');
elif (int(age))<10:
print('you are kid');
else:
print('you are adult');
###################################################
name = input('please input your name: ');
height = float(input('please input your height: '))
weight = float(input('please input your weight: '));
bmi = ((height)/(weight**2))*100;
print(bmi);
###
################################################
dic = {}
dic['one'] = "one"
dic[2] = "two"

tinydic = {'name': 'Robin', 'code' : 1458, 'dept':'R&D'}


print ("dict['one'] = ", dic['one']) # Prints value for 'one' key
print ('dict[2] = ', dic[2]) # Prints value for 2 key
print ('tinydict = ', tinydic) # Prints complete dictionary
print ('tinydict.keys() = ', tinydic.keys()) # Prints all the keys
print ('tinydict.values() = ', tinydic.values()) # Prints all the values