Mosh的Python教程练习1

输出病人信息(学习定义变量)
name="John Smith"
age=20
is_newpatient=True
//不能简单把三个相加打印,类型不同(int str bool
输出某人喜欢的颜色(学习输入)
name=input("What's your name? ")
favourite_color=input("What's your favourite color? ")
print(name+" likes "+favourite_color)
//可以把三个相加打印,类型相同(同int
输出年龄和变量类型(类型转换和打印变量类型)
***输出年龄(int强制类型转换)***
birth_year=int(input("Birth_year: "))
age=2020-birth_year
print(age)

birth_year=input("Birth_year: ")
age=2020-int(birth_year)
print(age)

***输出变量类型***
birth_year=input("Birth_year: ")
print(type(birth_year))//
age=2020-int(birth_year)
print(type(age))//
print(age)
磅和Kg的转化(类型转换)
weight_lbs = input("Weight (lbs): ")
weight_kg = float(weight_lbs) * 0.45
print(weight_kg)
字符串的引号使用注意
1''或者""来定义字符串
2 ''或者""在字符串中出现,造成不匹配:
如: str1='Gloria's house'   
     str1="Gloria's house"
     str2="Python Course for "beginners" "
     str2='Python Course for "beginners" '
3 多行文本用""" """或者''' '''
email="""
		Hi,Doggy

		This is my first Python class. I fancy you.

		Hope you watch this.

		Love from,
		Meow
"""
print(email)
索引
str="Python Course for Beginners"
print(str[0])     #结果:P
print(str[1])     #结果:y
print(str[-1])    #结果:s
print(str[:])     #结果:Python Course for Beginners
print(str[0:])    #结果:Python Course for Beginners
print(str[:3])    #结果:Pyt
print(str[0:3])   #结果:Pyt
print(str[1:-1])  #结果:ython Course for Beginner
another=str[1:5]  #结果:ytho
print(another)
字符串连接和格式化字符串
first="John"
last="Smith"
message=first+" ["+last+"] is a coder."
msg=f'{first} [{last}] is a coder.'
print(message)
print(msg)
# John [Smith] is a coder.
字符串方法
在这里插入代码片
发布了8 篇原创文章 · 获赞 0 · 访问量 283

猜你喜欢

转载自blog.csdn.net/qq_40733888/article/details/105004903