1.Python 标识符
在 Python 里,标识符由字母、数字、下划线组成。 在 Python 中,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。 Python 中的标识符是区分大小写的。 以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入。 以双下划线开头的 __foo 代表类的私有成员,以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,如 __init__() 代表类的构造函数。 Python 可以同一行显示多条语句,方法是用分号 ; 分开,如: >>> print 'hello';print 'runoob'; hello runoob
2.python变量类型
int(有符号整型)
long(长整型[也可以代表八进制和十六进制])
float(浮点型)
complex(复数)
name = 'python' id = '1006' # 字符串或串(String)是由数字、字母、下划线组成的一串字符。
1 # 定义 注意列表元素可重复 2 city = ['Guangzhou', 'Shanghai','Beijing', 'Beijing', 'Tianjin'] 3 4 # 列表检索 5 city_1 = city[1] # 根据元素序号检索列表中元素 6 7 city_end = city[-1] # 可用来检索列表最后的元素
1 # 只读,不能修改。用 () 标识 2 name = ('python', 'java', 'C', 'C#') 3 4 print(name[0]) 5 6 print(name[0:2]) # 输出 ('python', 'java')
1 dic = { 2 'name':'XXX', 3 'id' :"007", 4 1 :'ddd' 5 } 6 7 print(dic[1]) 8 print(dic['name'])
3.运算符号
基本运算符有 + ,-, *,/,%(取模),**(幂),//(取整除向,下取整)
=(赋值),==(判等),!=(不等于),<>(不等于),>(大于),<(小于),>=(大于等于),<=(小于等于)
4.条件语句
1 a = 5; b = 6; c = 0 2 if a > c: 3 print("a>c") 4 else: 5 print("a<c") 6 7 # 多个条件 8 if a>c: 9 print("a>c") 10 elif a == c: 11 print("a=c") 12 else: 13 print("a<c")
5.循环语句
1 for num in range(0, 10): 2 print(num) 3 >>0,1,2,3,4,5,6,7,8,9 4 5 for ss in "Apple": 6 print(ss) 7 >>A,p,p,l,e 8 9 fruits = ["Apple", "banana", "mango"] 10 for fruit in fruits: 11 print(fruit) 12 13 # 通过下标检索循环 14 for ff in range(len(fruits)): 15 print(fruits[ff]) 16 >>Apple,banana,mango
1 # while循环 2 num = 0 3 while num < 5: 4 print num 5 num += 1 6 >>0,1,2,3,4 7 8 9 # 无限循环 10 while True: 11 print("循环中。。。") 12 # break