码迷,mamicode.com
首页 > 编程语言 > 详细

python基础二

时间:2018-10-13 22:49:36      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:format   title   str   集合   解释   python解释器   关联   数据类型   copy   

1、格式化输出。

现有一练习需求,问用户的姓名、年龄、工作、爱好 ,然后打印成以下格式

技术分享图片
------------ info of Alex Li -----------
Name  : Alex Li
Age   : 22
job   : Teacher
Hobbie: girl
------------- end -----------------
技术分享图片

你怎么实现呢?你会发现,用字符拼接的方式还难实现这种格式的输出,所以一起来学一下新姿势

只需要把要打印的格式先准备好, 由于里面的 一些信息是需要用户输入的,你没办法预设知道,因此可以先放置个占位符,再把字符串里的占位符与外部的变量做个映射关系就好啦

技术分享图片
name = input("Name:")
age = input("Age:")
job = input("Job:")
hobbie = input("Hobbie:")

info = ‘‘‘
------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name 
Name  : %s  #代表 name 
Age   : %s  #代表 age  
job   : %s  #代表 job 
Hobbie: %s  #代表 hobbie 
------------- end -----------------
‘‘‘ %(name,name,age,job,hobbie)  # 这行的 % 号就是 把前面的字符串 与拓号 后面的 变量 关联起来 

print(info)
技术分享图片

%s就是代表字符串占位符,除此之外,还有%d,是数字占位符, 如果把上面的age后面的换成%d,就代表你必须只能输入数字啦

age     : %d

我们运行一下,但是发现出错了。。。技术分享图片

说%d需要一个数字,而不是str, what? 我们明明输入的是数字呀,22,22呀。

不用担心 ,不要相信你的眼睛我们调试一下,看看输入的到底是不是数字呢?怎么看呢?查看数据类型的方法是什么来着?type()

name = input("Name:")
age = input("Age:")
print(type(age))

执行输出是

Name:Alex
Age:22
<class ‘str‘> #怎么会是str
Job:IT

让我大声告诉你,input接收的所有输入默认都是字符串格式!

要想程序不出错,那怎么办呢?简单,你可以把str转成int

age = int(  input("Age:")  )
print(type(age))

肯定没问题了。相反,能不能把字符串转成数字呢?必然可以,str( yourStr )

问题:现在有这么行代码

msg = "我是%s,年龄%d,目前学习进度为80%"%(‘金鑫‘,18)
print(msg)

这样会报错的,因为在格式化输出里,你出现%默认为就是占位符的%,但是我想在上面一条语句中最后的80%就是表示80%而不是占位符,怎么办?

msg = "我是%s,年龄%d,目前学习进度为80%%"%(‘金鑫‘,18)
print(msg)

这样就可以了,第一个%是对第二个%的转译,告诉Python解释器这只是一个单纯的%,而不是占位符。

2、基本运算符。

运算符

  计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算,今天我们暂只学习算数运算、比较运算、逻辑运算、赋值运算

算数运算

以下假设变量:a=10,b=20

技术分享图片

比较运算

以下假设变量:a=10,b=20

技术分享图片

赋值运算

以下假设变量:a=10,b=20

技术分享图片

逻辑运算

技术分享图片

针对逻辑运算的进一步研究:

  1,在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。

例题:

判断下列逻辑语句的True,False。

技术分享图片
1,3>4 or 4<3 and 1==1
2,1 < 2 and 3 < 4 or 1>2 
3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
技术分享图片

  2 ,  x or y , x为真,值就是x,x为假,值是y;

             x and y, x为真,值是y,x为假,值是x。

 技术分享图片

例题:求出下列逻辑语句的值。

8 or 4
0 and 3
0 or 4 and 3 or 7 or 9 and 6
 1 #and or not
 2 #优先级,()> not > and > or
 3 # print(2 > 1 and 1 < 4)
 4 # print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
 5 # T or T or F
 6 #T or F
 7 # print(3>4 or 4<3 and 1==1)  # F
 8 # print(1 < 2 and 3 < 4 or 1>2)  # T
 9 # print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # T
10 # print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)  # F
11 # print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)  # F
12 # print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
13 
14 #ps  int  ----> bool   非零转换成bool True   0 转换成bool 是False
15 # print(bool(2))
16 # print(bool(-2))
17 # print(bool(0))
18 # #bool --->int
19 # print(int(True))   # 1
20 # print(int(False))  # 0
21 
22 
23 ‘‘‘x or y x True,则返回x‘‘‘
24 # print(1 or 2)  # 1
25 # print(3 or 2)  # 3
26 # print(0 or 2)  # 2
27 # print(0 or 100)  # 100
28 
29 
30 # print(2 or 100 or 3 or 4)  # 2
31 
32 # print(0 or 4 and 3 or 2)
33 ‘‘‘x and y x True,则返回y‘‘‘
34 # print(1 and 2)
35 # print(0 and 2)
36 print(2 or 1 < 3) #2
37 print(3 > 1 or 2 and 2) #True

 

in,not in :

判断子元素是否在原字符串(字典,列表,集合)中:

例如:

#print(‘喜欢‘ in ‘dkfljadklf喜欢hfjdkas‘)
#print(‘a‘ in ‘bcvd‘)
#print(‘y‘ not in ‘ofkjdslaf‘)

python基础二

标签:format   title   str   集合   解释   python解释器   关联   数据类型   copy   

原文地址:https://www.cnblogs.com/maystar/p/9784206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!