Mosh的Python教程练习1

简单的打印
print('*'*10) #打印:**********
输出病人信息(学习定义变量)
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.
字符串方法

区别:
1 函数(function):通用函数,如print()、len()称为函数。
2 方法(method):针对某一特定对象的函数,称为方法。如.upper()这个方法是针对string类型的,属于string类型特有的函数。

len()
course.upper()
course.lower()
course.title()
course.find()
course.replace()
"..." in course
# len() 打印字符串长度[后续len()还可以记录列表中item个数]
course="Python Course for Beginners"
print(len(course))
#Tips:空格也是要计数的
# upper() 字母全部转换成大/小写
course="Python Course for Beginners"
print(course.upper()) #打印:PYTHON COURSE FOR BEGINNERS
print(course.lower()) #打印:python course for beginners
print(course)         #打印:Python Course for Beginners
#Tips:转换大小写,并不对原来的字符串造成改变,这也是print(course)打印结果的原因
# find() 对应字符第一次出现的索引 【大小写敏感】
course="Python Course for Beginners"
print(course.find("e")) # 打印结果 12
print(course.find("O")) # 打印结果 -1 即:O没有在字符串中出现过
print(course.find("Beginners")) #打印结果 18 Beginner的第一个字符B在索引18处出现
#Tips:空格也占一个索引对应的位置
#replace() 替换指定字符或者字符串

#1 replace() 对单个字符替换
print(course.replace("P","J")) #打印:Jython Course for Beginners
print(course) #打印:Python Course for Beginners

#2 replace() 因大小写敏感,在原字符串中找不到该单词,不做替换
print(course.replace("beginners","Absolutely Beginners")) # 打印:Python Course for Beginners(字符串没有更改)
print(course) #打印:Python Course for Beginners

#3 在原字符串中找到单词,并替换(重新打印时,替换竟然没对原字符串产生影响)
course="Python Course for Beginners"
print(course.replace("Beginners","Absolutely Beginners"))  #打印:Python Course for Absolutely Beginners
print(course) #打印:Python Course for Beginners
#=================================================
#Doubts:
为什么使用replace将Beginners替换成Absolutely Beginners后,重新打印course这个字符串,而打印的结果还是未修改的course字符串?
------???难道是因为只是在本次print()中对course用replace方法对Beginners这个单词做了暂时的替换,所以没能对原来的course字符串产生影响吗?
------!!!测试:
course="Python Course for Beginners"
course.replace("Beginners","Absolutely Beginners")
print(course)
------打印结果:Python Course for Beginners
------结论:
course="Python Course for Beginners"
course=course.replace("Beginners","Absolutely Beginners")
print(course)
#打印: Python Course for Absolutely Beginners
#================================================
#查找某个字符串中是否出现了某个词或字母(利用in操作符(in operator),写一个产生布尔值的表达式(Boolean Expression))
course="Python Course for Beginners"
print("Python" in course) #打印:True
print("python" in course) #打印:False
#Differences:
1 find()返回的是 字符(character)或者字符序列(sequence of characters)的索引
2in的布尔表达式 返回的是 是否含有单词的结果(True or False)

参考地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值