1.first sentence-'Hello World’
#!/usr/bin/env python --系统默认下的 # !/usr/bin/python --原装的 print("hello world!")
2.变量的定义和赋值
(1)python是强类型定义语言,无需定义数据类型,直接根据赋值结果反向定义数据类型
name = "EleanorJiang" # string age = 0 # int salary = 2000.0000 # float
(2)变量的内存地址指向问题
name = "Eleanor Jiang" print(name) name2 = name # 指向name的内存地址的值 print(name2) print("What is the value of name2 now? name = ", name, "name2 = ", name2) print("before:", name2) name = "Eleanor Jiang2" # 重新定义变量,改变name值时,name2的值不变 print("after:", name2)
执行结果
(3)命名方式
name_of_GF = "friend1" # 使用下划线命名变量(python较多) nameOfGF = "friend2" # 使用驼峰式命名变量(java较多) PIE = 3.14 # 常量(static):Python 没有常量的概念,用大写表示常量,但是编译器方面对于此变量值是可以更改的
3.字符code的演变过程
# utf-8:英文1个字节 中文3个字节 属于unicode # ASCII: 255 1bytes # 1980 GB2312:7000+ # 1995 GBK1.0 2w+ # 2000 GB18030 2.7w+ # Unicode 2bytes # utf-8 en:1bytes,zh:3bytes
4.Python默认的字节编码
# python3 default:uft-8 # python2 default:ASCII
5.input 用户输入
name = input("What is your name?") # 用户输入 print("Hello " + name)
6.input 输入的值默认为string类型,如遇age,salary之类 数字类型需要将String类型强制转化为数字类型。
同时,数字类型也可以强制转化为string类型
name = input("What is your name?") # 用户输入 print("Hello " + name) age = input("What is your age?") print("before:" + str(type(age))) # 打印变量的数据类型 age = int(age) # 强制转换 python:强类型定义 print("after1:" + str(type(age))) age = str(age) print("after2:" + str(type(age))) age = int(age) job = input("What is your job?") salary = input("What is your salary?") salary = float(salary)
7.格式化字符串输出:
(1)使用%类占位符
# 格式化输出 占位符: %s(string) %d(double,验证数字类型) %f(float 20000->20000.000000) info = ''' --------info of %s------------ name:%s age:%d job:%s salary:%f ''' % (name, name, age, job, salary) print(info)
(2)使用{}和变量名作为占位符 例: {_name}与_name=name对应
info2 = ''' --------info2 of {_name}------------ name:{_name} age:{_age} job:{_job} salary:{_salary} '''.format(_name=name, _age=age, _job=job, _salary=salary) print(info2)
(3)使用{}与数字作为占位符,同一占位符可用多次 例: {0}可多次出现
info3 = ''' --------info3 of {0}------------ name:{0} age:{1} job:{2} salary:{3} '''.format(name, age, job, salary) print(info3)
8.关于注释
注释的快捷键: ctrl+/ 和 shift+"
"""多行注释""" '''多行注释''' # python中'和"完全一样 # 单行注释
9.调用其他模块或者标准库,使用import
import getpass # import 模块 or 标准库 getpass给密码加密 _username = 'Eleanor' _password = '123' username = input("username:") pwd = getpass.getpass("password:") # pycharm里getpass不好使 可以在dos界面执行 print(username, pwd)
dos执行结果
10.Python是强类型语言,强制缩进,省略了结构符,类似{}以及()
if _username == username and _password == pwd: # 强制缩进,省掉了结构符 print("Welcome user {name} login ".format(name=username)) else: print("Invalid username or password") # IndentationError缩进错误
11.if else的使用
age_of_Eleanor = 26 guessAge = input("Please guess age:") #输入猜测的年龄 guessAge = int(guessAge) if guessAge == age_of_Eleanor : print("Yes, you got it") elif guessAge > age_of_oldboy: # else if=elif print("rethink smaller") else: print("rethink bigger")
12.while的使用 (基本上和java一样 只是没有结构符-各种括号 而已,break和continue也是一样)
(1)while和break的结合使用
count = 0 while count < 100: print("count=", count) count = count + 1 if count == 99: break
执行结果:
(2)while和if-elif-else的结合使用
age_of_Eleanor = 26 guessAge = input("Please guess age:") guessAge = int(guessAge) count = 0 while guessAge != age_of_Eleanor: if count >= 3: print("no chance") break if guessAge == age_of_Eleanor: print("Yes, you got it") # 输入正确后直接break结束程序执行 break elif guessAge > age_of_Eleanor: # else if print("rethink smaller") else: print("rethink bigger") flag = input("keep going?Y or N :") # 输入错误时,由用户判断是否继续 if flag == "N": break guessAge = input("Please guess age:") guessAge = int(guessAge) count += 1 else: # while和else 可以联用 print("at the else of while-else") print("end of the while process")
执行结果(1):不满足while条件
如果break跳出来的时候 不满足while条件 执行while-else的else
即break跳出来时 guessAge=26
while条件 guessAge != age_of_Eleanor 不成立为false
执行while-else 中的 else,即"at the else of while-else"
执行结果(2):满足while条件
如果break跳出来的时候 满足while条件 不执行while-else的else
即break跳出来时 guessAge=22
while条件 guessAge != age_of_Eleanor 成立为true
不执行while-else 中的 else,即"at the else of while-else"不出现
13.for的使用
(1)for和continue的结合使用
1 for i in range(10): 2 if i == 7: 3 print("missing 7") 4 continue 5 print("i=", i)
(2)for循环的增量
1 for i in range(10): # 每次增加默认1,默认从0开始,即由0至10 2 print("loop:", i) 3 4 for i in range(0, 10, 2): # 从0开始到10结束,每次增加2 5 print("loop:", i)
(3)双层for循环(和java一样)
count = 1 for i in range(1, 10): for j in range(1, 10): print("No.", count, ": i=", i, ",j=", j) count += 1 if j >= 5: break
(4)for和if else的结合: for可以与else连用(与java不一样)
age_of_Eleanor = 26 guessAge = input("Please guess age:") guessAge = int(guessAge) for count in range(3): # 0,1,2,3:从0开始,共4次 if guessAge == age_of_Eleanor: print("Yes, you got it") break elif guessAge > age_of_Eleanor: # else if print("rethink smaller") else: print("rethink bigger") guessAge = input("Please guess age:") guessAge = int(guessAge) else: print("no chance") print("end of the for-else")
执行结果(1)猜中:break执行后,整个for-else退出
执行结果(2)未猜中四次后,for循环结束退出执行for-else中的else
14.课后练习
关于txt文件的读和写,参考文章:
python读取、写入txt文本内容
# _*_coding:utf-8_*_ # Author:Eleanor Jiang # 编写登陆接口 # 输入用户名密码 # 认证成功后显示欢迎信息 # 输错三次后锁定 import getpass file_name = "UserList.txt" # 读文件内容 # read():一次性读全部内容 with open(file_name, "r") as f: # 打开文件 data = f.read() # 读取文件 print(str(type(data))) print(data) # readline()读取第一行内容 with open(file_name, "r") as f: # 打开文件 data = f.readline() # 读取文件 print(str(type(data))) print(data) # readlines()读列表 with open(file_name, "r+") as f: # 打开文件 data = f.readlines() # 读取文件 print(str(type(data))) print(data) for i in data: infos = i.split(" ") print(str(type(data))) print(infos) print(infos[0]) # 输入用户名密码 input_username = input("Please enter your username:") input_password = getpass.getpass("Please enter your password:") count = 1 # print("username:", input_username, ",password:", input_password) MAX_COUNT = 3 default_username = "" default_password = "" lineNo_in_txt = 1 while count <= MAX_COUNT: if default_username == "": # 认证用户信息 for i in data: # 分割字符串 infos = i.split(" ") if infos[0] == input_username: default_username = infos[0] default_password = infos[1] locked_flag = not bool(infos[2].find("unlocked") >= 0) break lineNo_in_txt += 1 if default_username == "": # 检测账号是否存在 print("User not existed.") break if locked_flag: # 如果账号被锁定 print("Account:", input_username, " is locked.") break if input_username == default_username and input_password == default_password: # 认证成功后显示欢迎信息 print("Welcome to login,", input_username) break else: # 认证失败后剩余次数 print("Username or password may be incorrect, please try it again,", MAX_COUNT - count + 1, " chances left") count += 1 # input_username = input("Please enter your username:") input_password = getpass.getpass("Please enter your password:") else: # 输入三次后锁定 print("Account:", input_username, " is locked.") new_data = data new_data[lineNo_in_txt - 1] = new_data[lineNo_in_txt - 1].replace("unlocked", "locked") print("No ", lineNo_in_txt, "is updated") # 清空源文件 f = open(file_name, "r+") f.truncate() for i in new_data: f.write(i) f.flush() f.close()