Python—第1周—introduce
一.版本问题
推荐使用3.0的版本,2.0的版本已经不再更新了。且所有的包都已经支持了3.0
Python是一门解释性语言,优点简单,效率高,可移植,可扩展,可嵌入,缺点就是速度慢,代码不能加密,线程不能用多CPU
优点:有良好的平台兼容性,在任何环境中都可以运行,前提是安装了解释器(虚拟机)。灵活,修改代码的时候直接修改就可以,可以快速部署,不用停机维护。
缺点:每次运行的时候都要解释一遍,性能上不如编译型语言。
自带的解释器是CPython。如果要和java和net平台交互,应该通过网络调用,保持程序之间的独立性。
开发工具推荐使用Pycharm。因为有自动补全的功能。
二.Python安装
分为windows和linux
windows
1、下载安装包
https://www.python.org/downloads/
2、安装
默认安装路径:C:\python27
3、配置环境变量
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
如:原来的值;C:\python27,切记前面有分号
Linux和mac
无需安装。原装python环境,如果自带2.6请升级到2.7
三.第一个程序 hello world
可以在Cpython里面直接输入
print("Hello World!")
或者在新建一个.py的文件,使用python执行
vi hello.py
print("Hello world")
python hellp.py
或者/.hello.py 可执行文件,需要在hello.py头部指定解释器
#!/usr/bin/env python env找环境变量。如果你把3.5设置成环境变量后。直接使用新版本 #!/usr/bin/python 写死python环境变量,比如你自己安装一个3.5如果这么写。用的还是原装的2.7的环境。
四.变量,常量与字符编码
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
['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']
变量就是存储一个值来调用。
定义了一个变量,名字为name ,将dustin 赋予给name。 单引号和双引号的作用一样。
定义变量
name = 'dustin'
调用变量
print ('my name is ',name)
输出结果:which one is my name : dustin
name = 'dustin'
name2 = name
print('which one is my name:',name,name2)
输出结果:which one is my name: dustin dustin
有重新赋予name值后,name2的结果
name = "dustin"
name2 = name
print("which one is my name ",name,name2)
name = "Austin"
print("which one is my name ",name,name2)
输出结果:
which one is my name Dustin Dustin
which one is my name Austin Dustin
常量
大写变量名代表为常量,可以更改。但是不要改。因为他是常量。
字符编码
UTF-8,是对Unicode编码的压缩和优化,ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
python3 直接可以输出中文,python2 不可以。必须要声明字符编码集
#!/usr/bin/env python # -*- coding: utf-8 -*- 这样即可
注释
# 注释单行
''' ''' 三个单引号或者三个双引号注释多行
五.用户输入
%s字符 %d整数 %f小数点
使用input函数
name = input("what is your name ?")
print("Hello mr",name)
输出username和passwd
username = input("username:")
password = input("password:")
print(username,password)
对passwd加密(getpass在pycharm里不能用,可以使用debug模式来测试结果)
import getpass
username = input("please input your username : ")
pwd = getpass.getpass("please input your pwd : ")
print("username is :",username,"passwd is :",pwd)
输出结果
username is :dustin passwd is:123456
格式化输出1—使用%s占位符输出
name = input("name:")
age = input("age:")
job = input("job:")
info = '''
---------- info of %s ----------
name:%s
age:%s
job:%s
''' % (name,name,age,job)
print(info)
格式化输出2—推荐使用这个
name = input("name:")
age = int(input("age:"))
job = input("job:")
info = '''
---------- info of {_name} ----------
name:{_name}
age:{_age}
job:{_job}
''' .format(_name=name,
_age=age,
_job=job)
print(info)
结果
---------- info of dustin ----------
name:dustin
age:55
job:sre
格式化输出3
另一种
name = input("name:")
age = int(input("age:"))
job = input("job:")
info = '''
---------- info of {0}----------
name:{0}
age:{1}
job:{2}
''' .format(name,age,job)
print(info)
python默认将输入当作字符串处理,因为age是数字。所以必须强制int转换才可以
name = input("name")
age = int(input("age"))
job = input("job")
info = '''
---------- info of %s ----------
name:%s
age:%d
job:%s
''' % (name,name,age,job)
print(info)
打印age的类型
name = input("name:")
age = int(input("age:"))
job = input("job:")
info = '''
---------- info of %s ----------
name:%s
age:%d
job:%s
''' % (name,name,age,job)
print(info)
print(type(age))
---------- info of dustin ----------
name:dustin
age:54
job:sre
<class 'int'>
六.条件判断
逻辑判断:if else
多重判断:if elif else
案例1:判断用户名和密码正确就欢迎登录,错误就返回错误
_username = 'dustin'
_password = '123456'
username = input("username:")
password = input("password:")
if _username == username and _password == password:
print("welcome to new world")
else:
print("go to a hell now ")
案例2:猜测年龄,判断大小给予建议
my_age = 50
guss_age = int(input("guess age:"))
if my_age == guss_age:
print("you are right")
elif my_age > guss_age:
print("thinking big now ")
else:
print("thinking small")
结果:
guess age:33
thinking big now
guess age:66
thinking small
guess age:50
you are right
七.while循环和for循环
continue 跳出本次循环,进入下一次循环
break 结束循环
while循环
案例3:循环判断3次,到3次就跳出循环,猜对了也跳出循环
my_age = 88
count = 0
while True:
if count == 3:
break
guss_age = int(input("guess age :"))
if my_age == guss_age:
print("your are right!")
break
elif my_age > guss_age:
print("thinking big")
else:
print("thinking small")
count +=1
结果:
guess age :15
thinking big
guess age :99
thinking small
guess age :88
your are right!
guess age :88
your are right!
优化写法
my_age = 88
count = 0
while count < 3:
guss_age = int(input("guess age :"))
if my_age == guss_age:
print("your are right!")
break
elif my_age > guss_age:
print("thinking big")
else:
print("thinking small")
count +=1
猜错三次,给予提示。尝试太多次,注意count +=1不要写在else里面,应该与else对齐
my_age = 88
count = 0
while count < 3:
guss_age = int(input("guess age :"))
if my_age == guss_age:
print("your are right!")
break
elif my_age > guss_age:
print("thinking big")
else:
print("thinking small")
count +=1
else:
print("you have cried too many times,stop now !")
for循环
for i in range(10):
for j in range(10):
双层循环代表的就是走一次大循环,走十次小循环
打印0-9
for i in range(10):
print("loop:", i)
for i in range(0,10,1):
print("loop:", i)
结果: loop: 0 loop: 1 loop: 2 loop: 3 loop: 4 loop: 5 loop: 6 loop: 7 loop: 8 loop: 9
打印2 4 6 8
for i in range(0,10,2):
print("loop:", i)
loop: 0
loop: 2
loop: 4
loop: 6
loop: 8
案例4:改写while为for,猜年龄,三次后询问是否继续
my_age = 88
for i in range(3):
guss_age = int(input("guess age:"))
if my_age == guss_age:
print("you are right")
break
elif my_age > guss_age:
print("thinking big now ")
else:
print("thinking small")
else:
print("you have cried too many times,stop now !")
结果:
guess age:22
thinking big now
guess age:33
thinking big now
guess age:44
thinking big now
you have cried too many times,stop now !
询问是否继续
my_age = 88
count = 0
while count < 3:
guss_age = int(input("guess age:"))
if my_age == guss_age:
print("you are right")
break
elif my_age > guss_age:
print("thinking big now ")
else:
print("thinking small")
count += 1
if count == 3:
continue_confirm = input("Do you wanna try again? please input y or n")
if continue_confirm == 'y':
count = 0
elif continue_confirm == 'n':
break
结果:
guess age:22
thinking big now
guess age:33
thinking big now
guess age:44
thinking big now
Do you wanna try again? please input y or ny
guess age:22
thinking big now
guess age:33
thinking big now
guess age:44
thinking big now
Do you wanna try again? please input y or n
作业
作业一:博客 作业二:编写登陆接口 1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 作业三:多级菜单 三级菜单 可依次选择进入各子菜单 所需新知识点:列表、字典 作业 readme blog地址 程序的描述第一步第二步 流程图
由于学习的是老男孩alex老师的教程
所以以上内容有很多引用此链接:https://www.cnblogs.com/alex3714/articles/5465198.html

浙公网安备 33010602011771号