Mosh's Python Tutorial Exercise 1

The output of patient information (learning the definition of variables)
name="John Smith"
age=20
is_newpatient=True
//不能简单把三个相加打印,类型不同(int str bool
Someone like the color output (learning input)
name=input("What's your name? ")
favourite_color=input("What's your favourite color? ")
print(name+" likes "+favourite_color)
//可以把三个相加打印,类型相同(同int
Output age and variable types (type conversion and print variable types)
***输出年龄(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)
And transformation of pound Kg (conversion type)
weight_lbs = input("Weight (lbs): ")
weight_kg = float(weight_lbs) * 0.45
print(weight_kg)
Note the use of string quotes
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)
index
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)
Format string concatenation and
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.
String Methods
在这里插入代码片
Released eight original articles · won praise 0 · Views 283

Guess you like

Origin blog.csdn.net/qq_40733888/article/details/105004903