Python基础(一)
一、python的版本
python版本分为2系列和3系列,但是目前大部分企业都使用2系列版本,但是3系列python版本已经成为趋势,因此本博客主要以3系列的python版本为主。
二、变量
变量即是预先定义的值,主要用于方便脚本编写,方便脚本调用。变量通常以数字、字母及下划线开头,例:1_pass 、_pass 、pass都可以作为变量使用,但是特殊字符不可以
例:
print("hello world")
name = "xiao yu"
name2 = name
print("My name is ",name,name2)
name = "lala"
print("My name is ",name,name2) ##后面这个name会显示lala,而name2会显示xiaoyu,因为name2指向的是内存中xiaoyu这个位置
name = "xiaoyu"
msg = '''
name2 = name
print("My name is",name,name2)
'''
print(msg)
age = int(input("age:")) #integer
print(type(age) ,type(str(age)) )
job = input("job:")
salary = input("salary:")
info3 = '''
-------- info of {0}---
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
''' .format(name,age,job,salary)
print(info3)
三、字符编码与二进制
ASCII、unicode、utf-8、GB2312
四、用户交互
input命令:输入的所有字符都以字符串表示
例:name = input("name:")
print(type(name))
str
五、if、else流程判断
条件判断:如果、否则。例:
_username = 'xiaoyu'
_password = 'xiaoyu123'
username = input("username:")
#password = getpass.getpass("password:")
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password")
print(username, password)
六、while循环
例:
Age_of_Dulant=50
count = 0
while True:
if count == 3:
break
Guess_age = int(input("ips age:"))
if Guess_age == Age_of_Dulant:
print("very smart!")
break
elif Guess_age > Age_of_Dulant:
print("think smaller!")
else:
print("think bigger")
count += 1
Age_of_Dulant=50
count = 0
while count < 3:
Guess_age = int(input("ips age:"))
if Guess_age == Age_of_Dulant:
print("very smart!")
break
elif Guess_age > Age_of_Dulant:
print("think smaller!")
else:
print("think bigger")
count += 1
if count == 3:
continue_confirm = input("do you want to keep guessing:")
if continue_confirm != 'n':
count = 0
七、for循环
例:
#Age_of_Dulant=50
#for i in range(3):
# Guess_age = int(input("ips age:"))
# if Guess_age == Age_of_Dulant:
# print("very smart!")
# break
# elif Guess_age > Age_of_Dulant:
# print("think smaller!")
# else:
# print("think bigger")
#else: ##表示若上面for循环完成,则执行此else,否则,不执行
# print("you have try too many times,fuck off!")
#for i in range(10):
# if i < 4:
# print("loop:",i)
# else:
# continue
# print("hehe...")
for i in range(10):
print("_________:",i)
for j in range(10):
print(j)
if j > 5:
break