Long Way To Go 之 Python 1
Python是啥?
动态解释性的强类型定义语言。(看球不懂,慢慢理解...以下)
编程语言又有些撒子类型: 编译型、解释型
静态语言、动态语言
强类型定义语言、弱类型定义语言
编译型:(C/C++)
Complie: 有一个负责翻译的程序来对源代码进行转换,生成相对应的执行代码
Complier: 负责编译的程序
一次性把code转换成机器语言,然后写成可执行文件
解释型:(Java,Python)
如果说编译时一名书本翻译,解释更像一名同声传译
不断解释,不断执行... ...
动态类型语言:(Python Ruby)
运行期间才检查数据类型
静态类型语言:(C/C++,Java)
数据类型在编译时检查
强类型定义语言:
强制数据类型定义的语言
如没有强制转换,永远是同个数据类型
弱类型定义语言:
数据类型可以忽略的语言
一个变量可以赋予不同数据类型的值
小程序之 Hello World!
name = "你好,世界" # Python 3 可以写中文,但是不推荐 print(name)
小程序之 用户交互 (Interaction)
# user input name = input("name:") age = input("age:") job = input("job:") salary = input("salary:") # 4 种格式化输出 # Practice 1 格式化输出 forget it info = ''' -------- info of ''' + name +''' -------- Name: ''' + name + ''' Age; ''' + age + ''' Job: ''' + job + ''' Salary:''' + salary + ''' ''' print(info) # Practice 2 格式化输出 # in shell, %s = $ info2 = ''' ------- info of %s -------- Name: %s Age; %s Job: %s Salary: %s ''' % (name,name,age,job,salary) print(info) # %s s represents string # %d d only works for numbers Using int() to do transform for numerical variables # %f f is floating numbers # Practice 3 格式化输出 usually use this method info3 = ''' -------- info of {_name} -------- Name:{_name} Age:{_age} Job:{_job} Salary:{_salary} '''.format(_name=name, _age=age, _job=job, _salary=salary) print(info3) info4 = ''' -------- info of {0} -------- Name:{0} Age:{1} Job:{2} Salary:{3} '''.format(name, age, job, salary) print(info4)
getpass: 输入密码时,如想不可见,可用它
但python自带IDE(Integrated Development Environment)终端不支持隐藏回显
用windows下的cmd就OK~
import getpass _username = 'momo' _password = 'abc123' username = input("username:") password = getpass.getpass("password:") #encrypt # not working well in Pycharm but works in python
小程序之 猜年龄
流程: 1.定义一个年龄
2.用户去猜测
3.判断是否正确
---make only one guess---
if else:
age_of_oldboy = 56 # can only make one guess guess_age =int(input("guess age:")) # input 默认是string, need to use int() if guess_age == age_of_oldboy: print("yes, you got it!") elif guess_age > age_of_oldboy: print("think smaller!") else: print("think bigger!")
---make only 3 guesses---
while:
age_of_oldboy = 56 count = 0 while count < 3: # make only 3 guesses guess_age =int(input("guess age:")) if guess_age == age_of_oldboy: print("yes, you got it!") break elif guess_age > age_of_oldboy: print("think smaller!") else: print("think bigger!") count += 1 else: print("you have tried too many times...fuck off")
while True:
age_of_oldboy = 56 count = 0 while True: if count == 3: # only make 3 guesses print("you have tried too many times...fuck off") break guess_age =int(input("guess age:")) if guess_age == age_of_oldboy: print("yes, you got it!") break elif guess_age > age_of_oldboy: print("think smaller!") else: print("think bigger!") count += 1
for:
age_of_oldboy = 56 for i in range(3): # make only 3 guesses guess_age =int(input("guess age:")) if guess_age == age_of_oldboy: print("yes, you got it!") break elif guess_age > age_of_oldboy: print("think smaller!") else: print("think bigger!") else: print("you have tried too many times...fuck off")
---keep guessing---
age_of_oldboy = 56 count = 0 while count < 3: guess_age =int(input("guess age:")) if guess_age == age_of_oldboy: print("yes, you got it!") break elif guess_age > age_of_oldboy: print("think smaller!") else: print("think bigger!") count += 1 if count == 3: countine_confirm = input("do you wanna keep guessing?") if countine_confirm != 'n': count = 0
★★★★★添补小知识★★★★★
1.comments
两种方法:# comments
'''comments'''/"""comments"""
PS: ''' comments'''可以print多行;
但是 ' 和 " 只能print单行
2.解释器
# !/usr/bin/ python
告诉操作系统执行这个脚本时,调用/usr/bin下的python解释器
# !/usr/bin/env python
防止python没装在默认的/usr/bin 路径里,会到env设置里查找python的安装路径,再调用对应的路径下的解释器程序
3.编码申明
#-*- coding uft-8 -*-
可变长的编码集
4.Define variables
# Good try gf_of_oldboy = "Chen rong hua" GFOfOldboy = "Chen rong hua" # called hump hhhhh 23333 __________name = "Chen rong hua" # non sense 2name = "" a b # Define constants(in Capital) eg. PIE
Important!!!
name = "Momo" name2 = name print("My name is",name,name2) name = "PaoChe Ge" print(name,name2) # name2 does not change
5. int(), str()
# convert string into integer age = int(input("age:")) # convert integer into string print(str(age))
6. while, for 循环
while 循坏是地老天荒
例子:
# while True count = 0 while True: print("count:",count) count = count + 1 # same as count += 1 if count == 1000: break # for for i in range(10): # 0-10,0 gap print('-----------',i) for j in range(10): print(j) if j > 5: break
continue VS. break
不往下走 不往下走
进入下一个loop 跳出整个loop
7.bytes类型 (字节数据类型)
文本总是Unicode, 由str类型表示
二进制数据由bytes类型表示 (视频,音频文件)
8bit = byte(字节)
1024 byte = 1kbyte
1024kbyte = 1mbyte
1024mb = 1gb
1024gb = 1T
string --> encode --> bytes
bytes --> decode --> string
eg.
>>> '€20'.encode('utf-8')
b'\xe2\x82\xac20' # b开头代表二进制
>>> b'\xe2\x82\xac20' . decode('utf-8)
'€20'
msg = "我爱北京天安门" print(msg) print(msg.encode(encoding="utf-8").decode(encoding="utf-8"))
8. 三元运算
result = 值1 if 条件 else 值2 (一句话搞定)
9.进制