学习python Day 1

基础语句

改变变量的特性

weight = int(input('What is your weight(in pounds)? '))
kg_weight = weight * 0.49
print(str(kg_weight) + ‘kg’)

切换使用’ '和" “以及”"" “”"(换行展示)

course = ‘Python for “beginners”’
course = “Pyhon’s course for beginners”
course = ‘’‘Pyhon’s course for “beginners”’’’

[]

course = ‘Python for Beginners’
course[0] # returns the first character
course[1] # returns the second character
course[-1] # returns the first character from the end
course[-2] # returns the second character from the end
course[2:4]#返回第三个至第四个以前的所有字符
course[1:-1]#返回第二个及倒数第二个之前的
course[:]#默认第一个是0,后面是最后

f #format 格式化

first_name = “John”
last_name = “Smith”
msg = f’{first_name} [{last_name}] is a coder’
print(msg)

John [Smith] is a coder

len #计算字符长度

message.upper() # to convert to uppercase
message.lower() # to convert to lowercase
message.title() # to capitalize the first letter of every word
message.find(‘p’) # returns the index of the first occurrence of p (p可以是单词)

(or -1 if not found没有找到)
message.replace(‘p’, ‘q’)替换,注意大小写
‘Python’ in course #布尔函数:寻找Python这个词是否在course字符串中,返回布尔值

计算

/ # returns a float
// # returns an int
% # returns the remainder of division,余数
** # x ^ y

x = x + 10
x += 10

扫描二维码关注公众号,回复: 11063713 查看本文章

round #四舍五入取整函数
abs#绝对值

import math #导入函数math
math.函数(变量)

python 3 math module #搜索这个,可以看到函数的意义

IF 判断:

Elif 判断:
else:

is_hot = False
is_cold = True
if is_hot:
print(‘It is a hot day’)
print(‘Drink plenty of water’)
elif is_cold: #shift + tab 结束命令行
print(‘It is a cold day’)
print(‘Wear warm clothes’)
else:
print(‘It is a lovely day’)

逻辑语句

and
or
and not #后一个条件是false

比较

“>”
“==”
“<”
!=

print 举例

weight = int(input(“Please input your weight? “))
unit = input(”(L)bs or (K)g: “)
if unit.upper() == “L”:
print(f"Your weight is {weight*0.45} kg”)
else:
print(f"Your weight is{weight/0.45} pounds”)

print 自带回车换行

while语句

i = 1
while i <= 5:
i +=1
print(i)
print(“Done”)

重新命名变量 Shift + F6

发布了2 篇原创文章 · 获赞 0 · 访问量 36

猜你喜欢

转载自blog.csdn.net/Cauchy_chen/article/details/105699103