Python之路----------基础 一(数据类型、变量、基本语法、流程控制)
一、 数据类型与变量
1、数据类型
整数
1 #Python在程序中的表示方法和数学上的写法一模一样,-1,0,1都是整数。
浮点数
1 #浮点数就是小数。
字符串
1 #在Python中字符串是以单引号‘’或双引号“”括起来的任意文本。
字符串的常见操作:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 5 #1、移除空白 6 s = ' I am currently in a state of beginner Python ' 7 print(s.strip()) 8 #运行结果:I am currently in a state of beginner Python 9 10 #2、分割 11 s = 'I am currently in a state of beginner Python' 12 print(s.split(' ')) 13 #运行结果:['I', 'am', 'currently', 'in', 'a', 'state', 'of', 'beginner', 'Python'] 14 print(type(s.split(' '))) 15 16 #3、长度 17 s = 'I am currently in a state of beginner Python' 18 print(len(s)) 19 #运行结果:44 20 21 #4、索引 22 s = 'I am currently in a state of beginner Python' 23 print(s[0],s[1],s[-1]) 24 #运行结果:I n 25 26 #5、切片 27 s = 'I am currently in a state of beginner Python' 28 29 print(s[:]) #截取全部字符串 30 #运行结果 :I am currently in a state of beginner Python 31 32 print(s[0:3]) #截取第一位到第三位的字符 33 #运行结果 :I a 34 35 print(s[:-3]) #截取从头开始到倒数第三个字符之前 36 #运行结果 :I am currently in a state of beginner Pyt 37 38 print(s[6:]) #截取第七个字符到结尾 39 #运行结果 :urrently in a state of beginner Python 40 41 print(s[::-1]) #创造一个与原字符串顺序相反的字符串 42 #运行结果 :nohtyP rennigeb fo etats a ni yltnerruc ma I
布尔值
1 #与其他语言一样,布尔值只有两种True Flase
空值
1 #空值是None
列表(list)
list是一种有序的集合,可以随时添加和删除其中的元素。
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 #创建一个programming_languages列表 5 programming_languages = ['C', 'C++', 'JAVA'] 6 7 print(type(programming_languages)) 8 #运行结果:<class 'list'> ,programming_languages的类型为list 9 10 print(len(programming_languages)) 11 #运行结果:3 ,programming_languages的长度为3 12 13 print(programming_languages) 14 #运行结果:['C', 'C++', 'JAVA'] , 打印列表 15 16 print(programming_languages[2]) 17 #运行结果:JAVA , 通过索引访问programming_languages的数据 18 19 print(programming_languages.index('JAVA')) 20 #运行结果:2 , 通过index方法获取programming_languages列表制定元素索引 21 22 programming_languages.append('Python') 23 print(programming_languages) 24 #运行结果:['C', 'C++', 'JAVA', 'Python'] ,通过append方法向programming_languages添加新的元素 25 26 programming_languages.clear() 27 print(programming_languages) 28 #运行结果:[] ,通过clear方法清空programming_languages所有元素 29 30 programming_languages2 = programming_languages.copy() 31 print(programming_languages2) 32 #运行结果:['C', 'C++', 'JAVA'] ,通过copy方法复制一个新的list 33 34 print(programming_languages.count('JAVA')) 35 #运行结果:1 ,通过count方法计算元素在列表中出现的次数,如果元素不在列表中结果是0 36 37 programming_languages.remove('JAVA') 38 print(programming_languages) 39 #运行结果:['C', 'C++'] , 通过remove方法删除指定元素 40 41 programming_languages = ['C', 'C++', 'JAVA'] 42 programming_languages.pop() 43 print(programming_languages) 44 #运行结果:['C', 'C++'] , 通过pop方法删除列表最后一个元素 45 46 programming_languages = ['C', 'C++', 'JAVA'] 47 programming_languages.insert(2, 'PHP') 48 print(programming_languages) 49 #运行结果:['C', 'C++', 'PHP', 'JAVA'],通过insert方法在指定列表位置插入元素 50 51 programming_languages = ['C', 'C++', 'JAVA'] 52 programming_languages.reverse() 53 print(programming_languages) 54 #运行结果:['JAVA', 'C++', 'C'] ,通过reverse方法反转列表 55 56 programming_languages = ['C', 'C++', 'JAVA', 'Object-C', 'Python', 'Ruby'] 57 programming_languages.sort() 58 print(programming_languages) 59 #运行结果:['C', 'C++', 'JAVA', 'Object-C', 'Python', 'Ruby'],通过sort进行排序 60 61 programming_languages = ['C', 'C++', 'JAVA'] 62 programming_languages2 = ['ruby', 'PHP'] 63 programming_languages.extend(programming_languages2) 64 print(programming_languages) 65 #运行结果:['C', 'C++', 'JAVA', 'ruby', 'PHP'] ,通过extent方法接受列表作为参数,并将该参数的每个元素都添加到programming_languages列表中。
元组(tuple不可变列表)
1 #_*_coding:utf-8_*_ 2 3 #创建元组 4 ages = (11, 22, 33, 44, 55) 5 或者 6 ages = tuple((11, 22, 33, 44, 55)) 7 8 #元组只有两种方法count和index
字典(dict 无序)
1 #_*_coding:utf-8_*_ 2 3 #创建字典 4 5 dictionary = {'a': 1, 'b': 2, 'c': 3} 6 7 #通过pop方法删除指定key 8 dictionary.pop('a') 9 print(dictionary) 10 #运行结果:{'b': 2, 'c': 3} 11 12 #通过clear方法清空字典 13 dictionary.clear() 14 print(dictionary) 15 #运行结果:{} 16 17 #通过copy方法复制一个相同的字典 18 dictionary = {'a': 1, 'b': 2, 'c': 3} 19 dictionary2 = dictionary.copy() 20 print(dictionary2) 21 #运行结果:{'a': 1, 'b': 2, 'c': 3} 22 23 24 #通过fromkeys创建字典 25 #方式一: 26 dictionary_key = ['a', 'b', 'c', 'd', 'e'] 27 dictionary = dict.fromkeys(dictionary_key, '1') 28 print(dictionary) 29 #运行结果:{'c': '1', 'd': '1', 'a': '1', 'b': '1', 'e': '1'} 30 31 #方式二: 32 dictionary_key = ['a', 'b', 'c', 'd', 'e'] 33 dictionary = {} 34 print(dictionary.fromkeys(dictionary_key)) 35 #运行结果:{'c': None, 'd': None, 'a': None, 'b': None, 'e': None} 36 #上述两个方式没有区别,只是fromkeys一个是带有value,一个不带,不带的默认value为None 37 38 #获得key的值 39 dictionary = {'a': 1, 'b': 2, 'c': 3} 40 #方法一 41 print(dictionary.get('a')) 42 #运行结果:1 43 print(dictionary.get('d')) 44 #运行结果:None 45 #方法二 46 print(dictionary['a']) 47 #运行结果:1 48 #上面两个方法的区别是get方法中key不存在不会报错会返回None。 49 50 #字典的items方法 51 dictionary = {'a': 1, 'b': 2, 'c': 3} 52 print(dictionary.items()) 53 #运行结果:dict_items([('b', 2), ('a', 1), ('c', 3)]),常见用法如下: 54 for k, v in dictionary.items(): 55 print(k, v) 56 57 #字典的keys方法,获取字典key列表 58 dictionary = {'a': 1, 'b': 2, 'c': 3} 59 print(dictionary.keys()) 60 #运行结果:dict_keys(['c', 'b', 'a']) 61 62 #字典的update方法,是把另外一个字典的key/value添加到当前字典里面 63 dict = {'Name': 'Zara', 'Age': 7} 64 dict2 = {'Sex': 'female' } 65 dict.update(dict2) 66 print(dict) 67 #运行结果:{'Sex': 'female', 'Age': 7, 'Name': 'Zara'} 68 69 70 #字典的values方法,获取字典value列表 71 dictionary = {'a': 1, 'b': 2, 'c': 3} 72 print(dictionary.values()) 73 #运行结果:dict_values([3, 2, 1]) 74 75 #字典popitem方法,随机删除 76 dictionary = {'a': 1, 'b': 2, 'c': 3} 77 dictionary.popitem() 78 print(dictionary) 79 #运行结果:只有两组数据 80 81 #向字典里面添加元素,并给定默认值 82 dictionary = {'a': 1, 'b': 2, 'c': 3} 83 dictionary.setdefault('d', 4) 84 print(dictionary) 85 #运行结果:{'a': 1, 'd': 4, 'b': 2, 'c': 3}
2、变量
声明变量
1 #_*_coding:utf-8_*_ 2 3 a = 100 4 print(type(a)) 5 6 b = 'hello word' 7 print(type(b)) 8 9 c = True 10 print(type(c)) 11 12 13 d = [0,1] 14 print(type(d)) 15 16 e = (0,1) 17 print(type(e)) 18 19 f = {a:0,b:1,c:2} 20 print(type(f)) 21 22 #根据上面的运行结果我们发现,变量可以是任意类型的
变量命名规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
二、 用户输入
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 4 5 #name = raw_input("What is your name?") #only on python 2.x 6 name = input("What is your name?") 7 print("Hello " + name )
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import getpass 5 6 # 将用户输入的内容赋值给 name 变量 7 pwd = getpass.getpass("请输入密码:") 8 9 # 打印输入的内容 10 print(pwd)
三、数据运算
算数运算:

比较运算:

赋值运算:

逻辑运算:

成员运算:

身份运算:

位运算:

运算符优先级:

四、条件语句
1、if 条件
1 if <条件判断1>: 2 <执行1> 3 4 5 a = 8 6 if a>0 : 7 print('a大于0')
2、if ....else
1 if <条件判断1>: 2 <执行1> 3 else: 4 <执行2>
3、if...elif...else
1 if <条件判断1>: 2 <执行1> 3 elif <条件判断2>: 4 <执行2> 5 elif <条件判断3>: 6 <执行3> 7 else: 8 <执行4>
五、for 、while循环
最简单的for循环:
1 #_*_coding:utf-8_*_ 2 __author__ = 'Ashin Liu' 3 4 for i in range(10): 5 print("loop:", i )
while循环:
1 count = 0 2 while True: 3 print("你是风儿我是沙,缠缠绵绵到天涯...",count) 4 count +=1
不过死循环一般来说没有啥意义。。。。让我们改一下:
1 count = 0 2 while True: 3 print("你是风儿我是沙,缠缠绵绵到天涯...",count) 4 count +=1 5 if count == 100: 6 print("去你妈的风和沙,你们这些脱了裤子是人,穿上裤子是鬼的臭男人..") 7 break
来一个猜年龄的小游戏巩固一下:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 5 my_age = 30 6 7 count = 0 8 while count < 3: 9 user_input = int(input("input your guess num:")) 10 11 if user_input == my_age: 12 print("Congratulations, you got it !") 13 break 14 elif user_input < my_age: 15 print("Oops,think bigger!") 16 else: 17 print("think smaller!") 18 count += 1 #每次loop 计数器+1 19 else: 20 print("猜这么多次都不对,你个笨蛋.")
六、三元运算
1 ''' 2 result = 值1 if 条件 else 值2 3 4 如果条件为真:result = 值1 5 如果条件为假:result = 值2 6 ''' 7 8 a = 1 if 1 > 0 else 0 9 b = 1 if 1 < 0 else 0 10 print(a) 11 print(b)
浙公网安备 33010602011771号