Tsak2 数学运算、字符串、文本和列表

 先上代码(ps.注释用#号)

print("I will know count my chickens:")

print("Hens",25+30/6)

print("Roosters",100-25*3%4)

print("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 is False.")

print("How about some more.")

print("Is it greater?", 5 > -2)
print("Is it greater or equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)

#字符串的引用
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
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 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")

#个人信息录入
my_name = 'by'
my_age = 20 #not a lie
my_height = 175 #inches英寸
my_weight = 115 #lbs磅
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Bleak'

print(f"Let's talk about {my_name}.")
print(f"He's {my_height} inches tall.")
print(f"He's{my_weight} pounds heavy.")
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.")

# this line is tricky , try to get it exactly right
total = my_age + my_height + my_weight
print(f"If I add {my_age} , {my_height} , and {my_weight} I get {total}.")

#程序员的简短缩写
types_of_people = 10
x = f"There are {types_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}'")

hilarous = False
joke_evaluation = "Isn't that joke so funny?!{}"

print(joke_evaluation.format(hilarous))

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

print(w + e)

#列表
list = [ 'runoob', 786 , 2.23 , 'john' , 70.2 ]
tinylist = [123, 'john']

print(list)#输出完整列表
print(list[1])#输出列表第二个元素
print(list[1:3])
print(list[2:]) #输出从第三个到列表末尾所有元素
print(tinylist * 2) #输出列表两次
print(list + tinylist) #打印组合的列表

一、数学运算

最基本的加减乘除不等式就不说了,特别注意:%的作用是取余数,如 “4 % 2” 就是4除2余数,该值为0、 “25 * 3 / 4”就是75除4的余数,其值为3。

二、字符串和文本

好像也没啥好说的,定义字符串用单引号还是双引号?看要放到的位置,如果外面一层是单引号 里面那一层就是双引号,如果外面一层是双引号里面那一层就是单引号。(似乎幼稚的想法,等学深了再来看需不需要更正)。

三、列表

列表用 [ ] 标识,是 python 最通用的复合数据类型。

列表中值的切割也可以用到变量 [头下标:尾下标] ,就可以截取相应的列表,从左到右索引默认 0 开始,从右到左索引默认 -1 开始,下标可以为空表示取到头或尾。

个人思考:列表的检索其实有matlab那味了,但它这个数字比如1并不代表列表第一个元素,它是从0开始的,比如单纯输入print(list[1])就是输出列表的第2个元素,再比如,print(list[1:3])实际上不是第2个元素到第4个元素,它有这么一个奇妙的排列规则,我们把列表看成坐标轴,第1个元素占据了0到1之间的区间,第2个元素则是占据1到2之间的区间,故print(list[1:3])输出的是第2和第3个元素,因为1到3这个区间内只有第2和第3个元素。

补充:要注意千万不能把前面的变量或字符串打错,否则后面全部执行不了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值