counter = 100 # 一个整型数
miles = 999.99 # 一个浮点数
name = "Maxsu" # 一个字符串
site_url = "http://www.yiibai.com" # 一个字符串
print (counter)
print (miles)
print (name)
print (site_url)
五种标准数据类型
//创建数字对象
var1 = 10
var2 = 20
//删除一个或者多个
del var1, var2
[]
和[:]
)来获取字符串的子集(子字符串),其索引从字符串开始处的索引0
开始,并且以-1
表示字符串中的最后一个字符。str = 'yiibai.com'
print ('str = ', str) # Prints complete string
print ('str[0] = ',str[0]) # Prints first character of the string
print ('str[2:5] = ',str[2:5]) # Prints characters starting from 3rd to 5th
print ('str[2:] = ',str[2:]) # Prints string starting from 3rd character
print ('str[-1] = ',str[-1]) # 最后一个字符,结果为:'!'
print ('str * 2 = ',str * 2) # Prints string two times
print ('str + "TEST" = ',str + "TEST") # Prints concatenated string
[]
和[:]
)来访问,索引从列表开头的0
开始,并且以-1
表示列表中的最后一个项目。 加号(+
)是列表连接运算符,星号(*
)是重复运算符。list = [ 'yes', 'no', 786 , 2.23, 'minsu', 70.2 ]
tinylist = [100, 'maxsu']
print ('list = ', list) # Prints complete list
print ('list[0] = ',list[0]) # Prints first element of the list
print ('list[1:3] = ',list[1:3]) # Prints elements starting from 2nd till 3rd
print ('list[2:] = ',list[2:]) # Prints elements starting from 3rd element
print ('list[-3:-1] = ',list[-3:-1])
print ('tinylist * 2 = ',tinylist * 2) # Prints list two times
print ('list + tinylist = ', list + tinylist) # Prints concatenated lists
- 列表和元组之间的主要区别是 - 列表括在括号(`[]`)中,列表中的元素和大小可以更改,而元组括在括号(`()`)中,`无法更新`。元组可以被认为是`只读`列表。
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
{}
)括起来,可以使用方括号([]
)分配和访问值。dict = {}
dict['one'] = "This is one"
dict[2] = "This is my"
tinydict = {'name': 'maxsu', 'code' : 1024, 'dept':'IT Dev'}
print ("dict['one'] = ", dict['one']) # Prints value for 'one' key
print ('dict[2] = ', dict[2]) # Prints value for 2 key
print ('tinydict = ', tinydict) # Prints complete dictionary
print ('tinydict.keys() = ', tinydict.keys()) # Prints all the keys
print ('tinydict.values() = ', tinydict.values()) # Prints all the values
编号 | 函数 | 描述 |
---|---|---|
1 | int(x [,base]) | 将x转换为整数。如果x是字符串,则要base指定基数。 |
2 | float(x) | 将x转换为浮点数。 |
3 | complex(real [,imag]) | 创建一个复数。 |
4 | str(x) | 将对象x转换为字符串表示形式。 |
5 | repr(x) | 将对象x转换为表达式字符串。 |
6 | eval(str) | 评估求值一个字符串并返回一个对象。 |
7 | tuple(s) | 将s转换为元组。 |
8 | list(s) | 将s转换为列表。 |
9 | set(s) | 将s转换为集合。 |
10 | dict(d) | 创建一个字典,d必须是(key,value)元组的序列 |
11 | frozenset(s) | 将s转换为冻结集 |
12 | chr(x) | 将整数x转换为字符 |
13 | unichr(x) | 将整数x转换为Unicode字符。 |
14 | ord(x) | 将单个字符x转换为其整数值。 |
15 | hex(x) | 将整数x转换为十六进制字符串。 |
16 | oct(x) | 将整数x转换为八进制字符串。 |
序号 | 运算符 | 描述 |
---|---|---|
1 | is is not | 身份运算符 |
2 | in not in | 成员运算符 |
3 | not or and | 逻辑运算符 |
def changeme( mylist ):
//形参: 内部变量,修改不影响外部变量
mylist = [1,2,3,4] # This would assi new reference in mylist
print ("Values inside the function: ", mylist)
return
//外部变量
mylist = [10,20,30]
changeme( mylist )
print ("Values outside the function: ", mylist)
默认参数
def printinfo( name, age = 25 ):
"This prints a passed info into this function"
print ("Name: ", name, "Age ", age)
return
printinfo( age = 22, name = "Maxsu" )
printinfo( name = "Minsu" )
可变长度参数
def printinfo( arg1, *vartuple ):
"This prints a variable passed arguments"
print ("Output is: ", arg1)
for var in vartuple:
print (var, )
return
printinfo( 10 )
printinfo( 70, 60, 50 )
匿名函数
sum = lambda arg1, arg2: arg1 + arg2
print ("Value of total : ", sum( 10, 20 ))
print ("Value of total : ", sum( 20, 20 ))