第二章、数学、字符串、变量、列表基本使用方法

一、基础

(1) 实现第一行代码

print(Hello,world!")
认识注释,#注释有注释符号,以及在#后面相关的文字组成,代码不识别,不被运行,为了代码了解的更清楚。

# A comment, this is so you can read your program later.
# Anything after the # is ignored by python
print('Hello, world!') 

# You can also use a comment to "disable" or comment out code

print("This will run.")

(2) 数学运算

认识运算符,加减乘除取余小于等于大于等等。

1、+:加号
2、-:减号
3、/:斜杠
4、*:星号
5、%:百分号
6、<:小于号
7、>:大于号
8、<=:小于等于
9、>=:大于等于

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 equal?",5>-2)
print("Is it less or equal?",5<=-2)

msg_0 = 'hello world!'
msg_1 = 'hello python!'
print(msg_0,msg_1)

以下是输出的结果:

There are 10 types of people.
Those who know binary and those who don't.
I said:There are 10 types of people.
I also said:'Those who know binary and those who don't.'
Isn't that joke so funny?!False
This is the left side of...a string with a right side.

(3) 变量、字符串和文本

变量如何使用:

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.")

以下是输出结果:

There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
WE can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each  car.

字符串,变量以及各式的使用:

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}'")

hilarious = False
joke_evalution = "Isn't that joke so funny?!{}"

print(joke_evalution.format(hilarious))

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

print(w + e)

运行结果:

There are 10 types of people.
Those who know binary and those who don't.
I said:There are 10 types of people.
I also said:'Those who know binary and those who don't.'
Isn't that joke so funny?!False
This is the left side of...a string with a right side.

(4) 列表

列表是python使用最频繁的数据类型之一,可以完成多数集合类数据结构的实现,支持字符数字,字符串甚至可以报含列表字典等。
列表用[]表示,列表切片使用[头坐标:尾座标],截取需要的数据,从左到右由零开始;从右到左由-1开始。

lst_0 = ['runoob',345,67,20.3,'john']
tinylist = [123,'john']

print(lst_0)
print(lst_0[0])
print(lst_0[1:3])
print(lst_0[2:])
print(tinylist*2)
print(lst_0 + tinylist) 

输出结果:

['runoob', 345, 67, 20.3, 'john']
runoob
[345, 67]
[67, 20.3, 'john']
[123, 'john', 123, 'john']
['runoob', 345, 67, 20.3, 'john', 123, 'john']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值