笨办法学习Python3练习代码1-10
2024-09-02 18:58:58
ex1.py
print("hello world!",end = " ")#不换行
print("hello again")
print("I like typing this.")
print("This is fun.")
print("Yay!Printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')
print("")
ex2.py
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print("I could have code like this.")# and the comment after is ignored . # you can also use a comment to "disable" or comment out code:
#print("This won't run.")
print("This will run.")
print("hi # there.") # (#)的英文是octothorpe或者 pound character
ex3.py
#数字和数学运算
print("I will now count my chickens:")
print("hens", 25 + 30 / 6)
print("roosters", 100 - 25 * 3 % 4)
print("Now I will count the eggs:") print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) print("Is it true that 3 + 2 < 5 - 7?")
print(3 + 2 < 5 - 7)
print("what is 3 + 2 ?", 3 + 2)
print("what is 5 - 7?", 5 - 7)
print("Oh,that's why it's false.")
print("How about some more.")
print("Is it greater ?", 5 > -2)
print("Is it greater or eequal ?", 5 >= -2)
print("Is it less or equal ?", 5 <= -2)
ex4.py
#变量和命名 #汽车数量
cars = 100 #每辆汽车的运载人数
space_in_a_car = 4 #司机人数
drivers = 30 #乘客人数
passengers = 90 #空车数量
cars_not_driven = cars - drivers #占用的汽车数量
cars_driven = drivers #最大可运输的人数
carpool_capacity = cars_driven * space_in_a_car #capacity=容积,carpool=拼车 #平均每台汽车上的人数
average_passengers_per_car = passengers / cars_driven print("there are ",cars,"cars available.")
print("There are only",drivers,"drivers availlable.")
print("There will be ",cars_not_driven,"empty cars 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.")
ex5.py
name = 'Zed A.Shaw'
age = 35 #not a lie
height = 74 #inches
weight = 180 #lbs
eyes = 'Blue'
teeth = "White"
hair = "Brown" print(f"Let's talk about {name}.") #f代表格式化的意思format
print(f"He's {height} inches tall.")
print(f"He's {weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hair} hair.")
print(f"His teeth are usually {teeth} depending on the coffee.") #this line is tricky, try to get it exactly right
total = age + height + weight
print(f"If I add {age}, {height}, and {weight}. I get {total}.")
ex6.py
tpyes_of_people = 10 x = f"There are {tpyes_of_people} types of people."
binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}." print(x)
print(y)
print(f"I said: {x} .")
#还是格式化输出字符串,在输出的字符串外面带上''
print(f"I also said: '{y}'.") hilarious = "False"
joke_evaluation = "Isn't that joke so funny ?!{}" #输出Isn't that joke so funny ?!False
#.format()括号里面的内容都是格式化输出
print(joke_evaluation.format(hilarious)) w = "This is the left side of ..."
e = "a string with a right side."
print(w + e)
ex7.py
print("Mary had a little lamb.") print("Its fleece was white as {}.".format('snow'))
print("and everywhere that Mary went.")
print("." * 10) end1 = "c"
end2 = 'h'
end3 = 'e'
end4 = 'e'
end5 = 's'
end6 = 'e'
end7 = 'B'
end8 = 'u'
end9 = 'r'
end10 = 'g'
end11 = 'e'
end12 = 'r' print(end1 + end2 + end3 + end4 + end5 + end6, end = " ")
print(end7 + end8 + end9 +end10 + end11 + end12)
ex8.py
formatter = "{} {} {} {}" print(formatter.format(1,2,3,4))
print(formatter.format('one','two','three','four'))
print(formatter.format(True,False,False,True))
print(formatter.format(formatter,formatter,formatter,formatter)) print(formatter.format(
"Try your",
"Own text here",
"Maybe a poem",
"Or a song about fear")) #这个习题使用了format函数,让它返回formatter变量到其他字符串中
#当看到formatter.format(....),相当于
#1.取第一行定义的formatter字符串
#2.调用它的format函数
#3.给format传递四个参数,这些参数和formatter中的{}匹配
#4.在formatter上调用format的结果是一个新字符串,其中的{}被四个变量所替换
ex9.py
# Here's some new strange stuf(材料,原料),remember type it exactly.
days = "mon tue wed thu fri sat sun"
months = " jan\nfeb\nmar\napr\nmay\njun\njul\naug"
print("here are the days:",days)
print("Here are the months: ",months) print('''
There's something going on here.
with the three double-quotes.
we'll be able to tpye as much as we like.
even 4 lines if we want,or 5, or 6.''')
ex10.py
#\\转义字符\
#\t转义字符tab键缩进,水平制表符,一个制表符8个位置
#\'输出'
#\"输出“ 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)
最新文章
- js 抽奖转盘实现
- css-高度自适应的问题(body高度问题)
- ogre, dx, opengl坐标矩阵
- Linux下修改Oracle监听地址
- stand meeting
- spring jdbctemplate调用procedure(返回游标)
- 我常用的iphone开发学习网站[原创]
- sqlserver日志文件过大的处理方法
- GOLang(第二篇 发起一个Http请求)
- Window window = Window.GetWindow(控件)
- 深入研究EF Core AddDbContext 引起的内存泄露的原因
- CentOS下 SVN版本控制的安装(包括yum与非yum)的步骤记录。
- /usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFReadRGBAStrip@LIBTIFF_4.0’未定义的引用
- 关闭VS2017脚本调试 已启用 Visual Studio 中的 Chrome 脚本调试
- est是基于less的样式工具库
- 微信小程序的布局css样式
- jsp后台取出request请求头
- 真&#183;浅谈treap树
- Java如何显示工作日(周一至周五)的名称?
- JAVA并发:深入分析volatile