counter = 100 # 赋值整型变量
miles = 1000.0 # 浮点型
name = "John" # 字符串
print(counter)
print(miles)
print(name)
100 1000.0 John
Python支持四种不同的数字类型:
在内存中存储的数据可以有多种类型。Python有五个标准的数据类型:
截取字符串: [头下标:尾下表]
获取的子字符串包含头下标的字符,但不包含尾下标的字符。
str = 'Hello World!'
print(str) # 输出完整字符串
print(str[0]) # 输出字符串中的第一个字符
print(str[2:5]) # 输出字符串中第三个至第六个之间的字符串
print(str[2:]) # 输出从第三个字符开始的字符串
print(str * 2) # 输出字符串两次
print(str + "TEST") # 输出连接的字符串
Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print("list1[0]: ", list1[0])
print("list2[1:5]: ", list2[1:5])
list1[0]: physics list2[1:5]: [2, 3, 4, 5]
你可以对列表的数据项进行修改或更新,你也可以使用append() 方法来添加列表项,如下所示:
list = [] ## 空列表
list.append('Google') ## 使用 append() 添加元素
list.append('Runoob')
print(list)
['Google', 'Runoob']
可以使用 del 语句来删除列表的元素,如下实例:
list1 = ['physics', 'chemistry', 1997, 2000]
print(list1)
del list1[2]
print("After deleting value at index 2 : ")
print(list1)
['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]
总结列表的一些常见方法:
Python的元组与列表类似,不同之处在于元组的元素不能修改。 元组使用小括号(),列表使用方括号[]。 创建如下元组:
tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d"
Python的元组的操作类似于列表,这里不再赘述
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值 key=>value 对用冒号 :分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 ,格式如下所示:
键值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print("dict['Name']: ", dict['Name'])
print("dict['Age']: ", dict['Age'])
dict['Name']: Zara dict['Age']: 7
向字典添加新内容的方法是增加新的键/值对,修改或删除已有键/值对如下实例:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8 # 更新
dict['School'] = "RUNOOB" # 添加
print("dict['Age']: ", dict['Age'])
print("dict['School']: ", dict['School'])
dict['Age']: 8 dict['School']: RUNOOB
总结字典的一些常见方法:
定义一个函数只给了函数一个名称,指定了函数里包含的参数,和代码块结构。 这个函数的基本结构完成以后,你可以通过另一个函数调用执行,也可以直接从Python提示符执行。
# 定义函数
def printme( str ):
"打印任何传入的字符串"
print(str)
return
# 调用函数
printme("我要调用用户自定义函数!")
printme("再次调用同一函数")
我要调用用户自定义函数! 再次调用同一函数
以下是调用函数时可使用的正式参数类型:
#可写函数说明
def printme( str ):
"打印任何传入的字符串"
print(str)
return
#调用printme函数
printme()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-0a1894405b65> in <module> 6 7 #调用printme函数 ----> 8 printme() TypeError: printme() missing 1 required positional argument: 'str'
使用关键字参数允许函数调用时参数的顺序与声明时不一致,因为 Python 解释器能够用参数名匹配参数值。
#可写函数说明
def printme( str ):
"打印任何传入的字符串"
print (str)
return
#调用printme函数
printme( str = "My string")
My string
调用函数时,默认参数的值如果没有传入,则被认为是默认值。下例会打印默认的age,如果age没有被传入:
#可写函数说明
def printinfo( name, age = 35 ):
"打印任何传入的字符串"
print ("Name: ", name)
print ("Age ", age)
return
#调用printinfo函数
printinfo( age=50, name="miki" )
printinfo( name="miki" )
Name: miki Age 50 Name: miki Age 35
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
Python 编程中 if 语句用于控制程序的执行,基本形式为:
flag = False
name = 'luren'
if name == 'python': # 判断变量是否为 python
flag = True # 条件成立时设置标志为真
print('welcome boss' ) # 并输出欢迎信息
else:
print(name ) # 条件不成立时输出变量名称
luren
当判断条件为多个值时,可以使用以下形式:
python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现
num = 5
if num == 3: # 判断num的值
print('boss')
elif num == 2:
print('user')
elif num == 1:
print ('worker')
elif num < 0: # 值小于零时输出
print('error')
else:
print ('roadman') # 条件均不成立时输出
roadman
循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式:
Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
print ("Good bye!")
The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye!
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for letter in 'Python': # 第一个实例
print("当前字母: %s" % letter)
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print ('当前水果: %s'% fruit)
print ("Good bye!")
当前字母: P 当前字母: y 当前字母: t 当前字母: h 当前字母: o 当前字母: n 当前水果: banana 当前水果: apple 当前水果: mango Good bye!