python基础学习(一)
python简介
python的创始人为吉多·范罗苏姆(Guido van Rossum),诞生时间1989年圣诞
一、变量的命令规则
1.变量只能由大小写字母、数字和下划线三部分组成,并且不能以数字开头
2.变量名一定不能是关键字
比如:pass if else print import.......
二、python注释
1.单行注释:#
2.多行注释:''' content ''' 或者 """ content """
三、python的数据类型
1.int
2.long
3.float
4.布尔型
5.字符串
字符串相关的操作:
1.移除空白:str.strip('\n')
默认去除空白,需要去除什么,直接在strip('')里面写入即可
四、列表
name_list = ['zhangsan','lisi','tenglan']
name_list
type(name_list)
列表的基本操作:
1.索引 name_list[2]
2.切片 name_list[0:2]
3.追加 name_list.append('alex')
name_list.extend(['alex','jack'])
name_list.insert(1,'lilei')
4.删除 name_list.pop() name_list.remove('alex')
5.长度 len(name_list)
6.循环 for i in name_list:
如果一个列表中有多个alex,我们要全部删除:
for i in range(name_list.count('alex')):
name_list.remove('alex')
7.包含
8.索引值的个数:name_list.count('alex')
9.某个值的索引:name_list.index('jack')
10.排序:name_list.sort()
反转:name_list.reverse()
注意:range(1)
此处range相当于是一个迭代器,和python2.7不同
五、元组:
1.索引
2.切片
3.t.count('lisi'):lisi在元组中出现的次数
4.t.index('lisi'):lisi在元组的下标
六、按位运算
1.逻辑与:&
2.逻辑或:|
3.异或:^
4.取反:~
5.左移:<< 向左移右边补0
6.右移: >> 向右移左边补0
七、逻辑运算符
1.逻辑与:and(布尔与)
2.逻辑或:or(布尔或)
3.逻辑否:not(布尔非)
八、成员运算符:
1.in
2.not in
九、身份运算符
用于比较两个对象的存储单元
1.is:判断两个标识符是不是引用自一个对象
2.is not:判断两个标识符是不是引用自不同对象
十、文件的操作:
1.打开文件
file_obj = file('文件路径','模式')(python3中file已经不可用)
file_obj = open('文件路径','模式')
打开文件的模式
1.r:以只读方式打开文件
2.w:打开一个文件只用于写入,如果该文件存在则覆盖
3.a:打开一个文件用于追加,如果该文件已经存在,则在文件末尾进行追加,如果文件不存在则新建
4.w+:可写可读
四、模块
1.getpass
第一个模块:getpass [root@MiWiFi-R1CM-srv preview]# cat test_pass.py #!/usr/bin/evn python3 import getpass username = input("username:") passwd = getpass.getpass("password:") print(username,passwd) [root@MiWiFi-R1CM-srv preview]# python3 test_pass.py username:li yue mei password: li yue mei 000000 有一点需要注意:在导入模块的时候只需要写文件名即可,不需要加py >>> import test_pass.py username:nihao password: nihao 000000 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'test_pass.py'; 'test_pass' is not a package
2.os
>>> import os >>> os.mkdir('zhanglei') >>> cmd_res = os.popen('df -hT').read() >>> print(cmd_res) Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root ext4 18G 2.2G 15G 14% / tmpfs tmpfs 242M 0 242M 0% /dev/shm /dev/sda1 ext4 485M 33M 427M 8% /boot
3.sys
>>> import sys >>> sys.path ['', '/usr/local/python/lib/python35.zip', '/usr/local/python/lib/python3.5', '/usr/local/python/lib/python3.5/plat-linux', '/usr/local/python/lib/python3.5/lib-dynload', '/usr/local/python/lib/python3.5/site-packages'] >>> sys.version '3.5.1 (default, Mar 27 2016, 00:36:55) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)]' >>> sys.copyright 'Copyright (c) 2001-2015 Python Software Foundation.\nAll Rights Reserved.\n\nCopyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 1995-2001 Corporation for National Research Initiatives.\nAll Rights Reserved.\n\nCopyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nAll Rights Reserved.' >>> sys.maxsize 9223372036854775807 >>> sys.prefix '/usr/local/python' >>> sys.platform 'linux' 建议自己写的python模块存放目录:/usr/local/python/lib/python3.5/site-packages
四、循环
1.if....else
name = 'alex' passwd = 'alex3714' username = input('please input username:') password = input('please input password:') if username == name: print('用户名输入正确。') if password == passwd: print('密码输入正确') print('恭喜你,用户名和密码全部输入正确。') else: print('密码输入错误') else: print('呵呵,连用户名都没有输对,你太笨了。')
改进后的程序:
name = 'alex' passwd = 'alex3714' username = input('please input username:') password = input('please input password:') if username == name and password == passwd: print("Welcome Login !") else: print('Invaild username or password,please input again!')
2.if...elif...else
age = 18 guess_num = int(input('please input you guess number:')) if guess_num == age : print('Congruations.You got it.') elif guess_num > age: print('Think Smaller.') else: print('Think Bigger.')
程序执行结果如下:
猜对了: C:\python35\python3.exe D:/PythonS13/Day1/elif程序.py please input you guess number:18 Congruations.You got it. 猜小了: C:\python35\python3.exe D:/PythonS13/Day1/elif程序.py please input you guess number:12 Think Bigger. 猜大了: C:\python35\python3.exe D:/PythonS13/Day1/elif程序.py please input you guess number:20 Think Smaller.
3.for循环和break的使用
age = 18 for i in range(10): if i < 3: guess_num = int(input('please input you guess number:')) if guess_num == age : print('Congruations.You got it.') break #不往后走了,跳出整个循环 elif guess_num > age: print('Think Smaller.') else: print('Think Bigger.') else: print('Too mang time attempts!') break