Python3学习笔记(二)by Learn Python 3 the HARD WAY

  • python运算优先级

PEMDAS 括号(parentheses)指数(exponents)乘(multipilication)除(division)加(addition)减(subtraction)

值得注意的是这不是一个绝对优先级,可以这么理解PE(M&D)(A&S)

  • 变量与命名
cars = 100
#有一百辆车
space_in_a_car = 4.0
#一个车有四个空间
drivers = 30
#有三十个司机
passengers = 90
#有九十个乘客
cars_not_driven = cars - drivers
#cars_not_driven没有司机的车
cars_driven = drivers
#cars_driven有司机的车
carpool_capacity = cars_driven * space_in_a_car
#所有车的容量
average_passengers_per_car = passengers / cars_driven
#平均每辆车有几个乘客
print("There are", cars, "cars available.")
print("There are only", drivers, "drivers available.")
print("There will be", cars_not_driven, "empty car today.")
print("We can transport", carpool_capacity, "people today.")
print("We have", passengers, "to carpool today.")
print("We need to put about", average_passengers_per_car, "in each car.")

poweshell

PS C:\\Users\\ARAN\\lpthw> python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty car today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car.
  • 字符串的使用
my_name = 'Zed A. shaw'
my_age = 35
my_height = 74 #m
my_weight = 180 #kg
my_eyes = 'blue'
my_teeth = 'White'
my_hair = 'brown'
my_height2 = 100 * my_height #my_height2==mm
my_weight2 = 2 * my_weight #my_weight2==g
# test print (f"{my_height2}")

print(f"Let's talk about {my_name}.")
print(f"He's {my_height2} inches tall")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"His teeth are usually {my_teeth} depending on the coffee.")

total = my_age + my_height + my_weight

print(f"If I add {my_age}, {my_weight2}, and {my_height2} I get {total}."

powershell

PS C:\\Users\\ARAN\\lpthw> python ex5.py
Let's talk about Zed A. shaw.
He's 7400 inches tall
Actually that's not too heavy.
He's got blue eyes and brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 360, and 7400 I get 289.
  • round四舍五入函数,示例
f = round(1.77)
print(f"{f}")

输出2

打印 打印

type_of_people = 10
x = f"There are {type_of_people} type of types of people."
#打印字符
#如果遇到字符里面有{字符串},前面需要加f
binary = "binary"
do_not = "don't"
#定义bianry
#定义do_not
y = f"Those who know {binary} and those who {do_not}."
#
print(x)
#输出定义的字符不用加引号
#也可以使用format()来表示
print(y)

print(f"i said: {x}")
print(f"I also said: {y}")
#带{}的前面一定要加f

hilarious = False
#false不用加引号
joke_evaluation = "Isn't taht joke so funny?!{}"

print(joke_evaluation.format(hilarious))

w = "This is the left side of..."
e = "a string with a right side"

print(w + e)
a = 1
b = 2
c = a + b
print(c)

a_b = 1
a_b_c = a_b + 1
print(a_b_c)

a = "I"
b = "LOVE"
c = "CHINA"
d = a + b + c
e = "You"
print(d)
#字符前面加引号,数字不必加引号
print(f"If I told who loves {c}, I hope the one is {e}. ")
f = "If i told who loves {}"
#使用.format而不使用f()的时候应该在前面的语句上末尾加入{}
g = "I hope the one is {}"
print(f.format(c), g.format(e))

h = True
print(h)

打印 打印 打印

print("Mary had a little lamb")
print("Its fleece was white as {}.".format('snow'))
print("And everywhere thar Mary went")
print("." * 10) #What's that do?

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

#watch that comma at the end. try removing it to see what happens
print(end1 + end3 + end4 + end5 + end6, end=' ')
print(end7 + end8 + end9 + end10 + end11 + end12)
  • 如果不想python以另起一行的形式打印,可以在末尾加, end=‘ ‘

’_‘中间可以是变量,也可以是字符串,也可以是空格

  • 另外,字符串可以乘数字以输出更多字符

例如 ”.“ * 10

formatter = "{} {} {} {}"

print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
print(formatter.format(True, False, True, False))
print(formatter.format(formatter, formatter, formatter, formatter))
print(formatter.format(
    " Try your\\n",
    "Own text here\\n",
    "Maybe a poem\\n",
    "Or a song about fear "
))
  • 打印 打印 打印
#Here's some new strange stuff, remember type it exactly

days = "Mon Tue Wed Thu Fri Sat Sun"
months = "\\nJan\\nFeb\\nMar\\nMay\\nJUn\\nJul\\nAug"

print("Here are the days:", days)
print("Here are the months:", months)

print("""There is something going on here.With the three double-quotes.We'll be albe to type as much as we like.Even 4 lines if we want, or 5, or 6.
""")
  • 三个引号”“”代表超长打印,可以放很长很长的字符串
  • 转义字符
tabby_cat = "\\tI'm tabbed in."
persian_cat = "I'm split\\non a line"
backslash_cat = "I'm \\\\ a \\\\ cat"

fat_cat ="""
I'll do a list:
\\t* Cat food
\\t* Fishies
\\t* Catnip\\n\\t* Grass
"""

print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)

转义序列

  • input函数
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weight?", end=' ')
weight = input()

print(f"So, you're {age} old, {height} tall and {weight} heavy.")
  • 可以在input函数中加入问题,因此上面的问法可以替换为
age = input("How old are you? ")
height = input("How tall are you? ")
weight = input("How much do you weight? ")

print(f"So, you're {age} old, {height} tall and {weight} heavy.")
  • 参数、解包和变量
from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv

print(">>>> argv = ",repr(argv))

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second cariable is:", second)
print("Your third variable is:", third)
  • 写一个类似《Zork》的文字游戏
from sys import argv

script, user_name = argv
prompt = '> '
print(f"Hi {user_name}, I'm the {script} scripter")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
likes = input(prompt)

print(f"Where do you live {user_name}")
live = input(prompt)

print(f"What kind of computer do you have?")
computer = input(prompt)

print(f"""
Alright, so you said {likes} about liking me.
You live in {live}.
Not sure where that is.
And you have a {computer} computer.
Nice.
""")

值得注意的是在里面把提示用户输入的>定义为了prompt(提示),到时候直接修改一个自定义即可完成全局修改

  • 尽量不要把代码中的内容写死(hardcode),让用户来决定写哪个文件
  • 读取ex15_sample.txt中的文本,代码如下
from sys import argv

script, filename = argv

txt = open(filename)

print(f"Here's your file {filename}:")
print(txt.read())
print("\\n")

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read())
  • 只用input读取并打印代码
filename = input("please typing your filename\\n >>>")
txt = open(filename)
print(txt.read())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值