把《Python 编程快速上手》过了一遍。
Python很类似于shell脚本,不太区分变量类型,但复杂数据结构上考虑的不错,列表、元组、字典,基本上可以涵盖各种数据组织模型
1、python基础
1)数学操作符 ** % // / * - +
//:整除 **:乘方
2)数据类型 整型 浮点型 字符串
3)字符串连接和复制
连接 ‘xxx’+’yyy’
复制 ‘xxx’ * int
+ * 字符操作符
4)变量和赋值
同类型可加,异类型不能加。
无效的变量名
5)第一个程序
test_101.py 输入名字和年龄,并显示
# This program says hello and asks for my name.Name
import sys
print('len argv:' + str(len(sys.argv)))
if len(sys.argv) >= 2:
print("hello world %s" % sys.argv[1])
else:
print ('Hello , world !!')
print ('你的名字?What is your name?') #ask for their name
myName = input ()
print ('It is good to meet you ,' + myName)
print(len(myName))
print("What is your age?") #ask for their age
myAge = input()
print('You will be '+str(int(myAge) + 1) + ' in a year.')
6)函数
注释 #
print()函数
input()函数
变量名及输出
len()函数
str()、int()、float()
int()不能转换浮点的字符串,但能转换浮点数。
2、Python控制流
1)布尔值 True False 首字母大写
2)比较操作符
== != < > <= >=
3)布尔操作符 and or not
优先级 先not 次and 后or
4)混和布尔操作符和比较操作符
5)控制流的元素 条件语句和代码块
测试程序 :test_201.py
name = 'Mary'
sex = 'man'
password = 'swordfish'
if name == 'Mary':
print('Hello, Mary')
if password == 'swordfish':
print('Access granted.')
else:
print('Wrong password.')
elif sex == 'woman':
print('Error Name!! Girl')
else :
print('Error name!! Boy')
6)程序执行
7)控制流语句
测试程序:test_202.py
name = 'Carol'
#name = 'Alice'
age = 30
if name == 'Alice':
print('Hi, Alice')
elif age < 12:
print('You are not Alice,kiddo.')
elif age > 2000:
print('Unlike you, Alice is not an undead,immortl vampire.')
elif age > 100:
print('You are not Alice, grannie')
else :
print('You are a adult.')
while age < 35:
print('Hello world !' + name + ' ' + str(age))
age = age + 1
print ('My name is ')
for i in range(0,10,2):
print('Jimmy Five Times (' + str(i) +')')
name = ''
while name != 'your name':
print('Please type your name:')
name = input()
if name == 'exit':
break
print('Thank you!')
If语句 关键字 条件 冒号 缩进的代码块
else语句 关键字 冒号 缩进的代码块
elif语句 关键字 条件 冒号 缩进的代码块
while语句 关键字 条件 冒号 缩进的代码块
break语句
continue语句
for循环
range()函数 开始、停止、步长
8)导入模块
测试程序 test_203.py
import random,sys
for i in range(5):
print(random.randint(1,10))
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed ' + response + '.')
import 模块名
sys.exit()
9)猜数字
测试程序 test_204.py
#This is a guess the number game.
import random
secretNumber = random.randint(1,20)
print('I am thinking of a number between 1 and 20.')
#Ask the player to guess 6 times.
for guessesTaken in range(1,7):
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low')
elif guess > secretNumber:
print('Your guess is too high.')
else:
break # This condition is the correct guess!
if guess == secretNumber:
print('Good job! You guesssed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))
10)石头剪刀布
测试程序 test_106.py
import random,sys
print("ROCK, PAPER, SCISSORS")
#These variables keep track of the number of wins,losses ,and ties.
wins = 0
losses = 0
ties = 0
while True: # The main game loop.
print('%s Wins, %s Losses, %s Ties' %(wins,losses,ties))
while True: # The player input loop
print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit')
playerMove = input()
if playerMove == 'q':
sys.exit() #Quit the program
if playerMove == 'r' or playerMove == 'p' or playerMove == 's':
break #Break out of the player input loop.
print('Type on of r,p,s,or q.')
# Display what the player chose
if playerMove == 'r':
print('ROCK versus...')
elif playerMove == 'p':
print('PAPER versus...')
elif playerMove == 's':
print('SCISSORS versus...')
# Display what the computer chose:
randomNumber = random.randint(1,3)
if randomNumber == 1:
computerMove = 'r'
print('ROCK')
elif randomNumber == 2:
computerMove = 'p'
print('PAPER')
elif randomNumber == 3:
computerMove = 's'
print('SCISSORS')
# Display and record the win/loss/tie:
if playerMove == computerMove:
print('It is a tie!')
ties = ties +1
elif playerMove == 'r' and computerMove == 's':
print('You win!')
wins = wins + 1
elif playerMove == 'p' and computerMove == 'r':
print ('You win!')
wins = wins + 1
elif playerMove == 's' and computerMove == 'p':
print('You win!')
wins = wins + 1
elif playerMove == 'r' and computerMove == 'p':
print('You lose!')
losses = losses + 1
elif playerMove == 'p' and computerMove == 's':
print('You lose!')
losses = losses + 1
elif playerMove == 's' and computerMove =='r':
print('You lose!')
losses = losses + 1