Python基础 整形、布尔值、if条件判断、while循环、运算符、格式化输出

1,计算机基础。
2,python历史。

宏观上:python2 与 python3 区别:
python2 源码不标准,混乱,重复代码太多,
python3 统一 标准,去除重复代码。
3,python的环境。

编译型:一次性将所有程序编译成二进制文件。
缺点:开发效率低,不能跨平台。
优点:运行速度快。
:C,C++等等。

解释型:当程序执行时,一行一行的解释。
优点:开发效率高,可以跨平台。
缺点:运行速度慢。
:python ,php,等等。

4,python的发展。
5,python种类。

运行第一个py文件:
python3x :python 文件路径 回车
python2x :python2 文件路径 回车
python2 python3 区别:python2默认编码方式是ascii码
解决方式:在文件的首行:#-*- encoding:utf-8 -*-
python3 默认编码方式utf-8


6,变量。
变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。
1,必须由数字,字母,下划线任意组合,且不能数字开头。
2,不能是python中的关键字。
['and', 'as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'exec',
'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print',
'raise', 'return', 'try', 'while', 'with', 'yield']
3,变量具有可描述性。
4,不能是中文。
7,常量。
一直不变的量。 π
BIR_OF_CHINA = 1949


8,注释。
方便自己方便他人理解代码。
单行注释:#
多行注释:'''被注释内容''' """被注释内容"""


9,用户交互。input
1,等待输入,
2,将你输入的内容赋值给了前面变量。
3,input出来的数据类型全部是str

10,基础数据类型初始。
数字:int 12,3,45
+ - * / **
% 取余数
print(**,type('**'))
字符串转化成数字:int(str) 条件:str必须是数字组成的。
数字转化成字符串:str(int)
字符串:str,python当中凡是用引号引起来的都是字符串。
可相加:字符串的拼接。
可相乘:str * int
bool:布尔值。 True False。


11,if判断语法。(四种结构)

①if 条件:
结果

②if条件:

结果1

else:

结果2

(if条件成立就输出,条件不成立执行else。)

③if条件1:

结果1

 elif条件2:

结果2

  ...

  ...

 elif条件n:

结果n

(只会输出一种结果)

④if条件1:

结果1

    elif条件2:

结果2

. . .

. . .
 else:

结果n

重点:

if 的嵌套:

code = input('请输入验证码:')
username = input('请输入用户名:')
password = input('请输入密码:')
if code == 'qwe3':
    if username == 'alex' and password == '123':
        print('登陆成功')  
    else:
        print('用户名或者密码错误')
else:
    print('验证码错误')
View Code

12,while。

while 条件:
循环体


无限循环。while  1:
终止循环:1,改变条件,使其不成立。
2,break

3,调用系统命令:quit() exit() (不建议使用)

while 与 break 、 continue 之间的关系

 break:循环中遇到break 直接退出循环:

print(111)
while True:
    print(222)
    print(333)
    break
    print(555)
print(666)
View Code

continue: 结束本次循环,继续下一次循环

while True:
print(111)
print(222)
continue
print(333)
View Code

 练习题:

练习一:输出 1-100 内的所有奇数

count = 0
while count <= 100:
    if count%2 == 1:
        print(count)
    count += 1

练习二:输出 1-100 内的所有奇数

count = 0
while count <= 100:
    if count%2 == 0:
        print(count)
    count += 1

练习三:求1-2+3-4+5 ... 99所有数的和

1 count = 0
2 j = 0
3 while count <= 100:
4     if count%2 == 1:
5         j = j+count
6     else:
7         j = j-count
8     count+= 1
9 print(j)
View Code

练习四:用户登录菜单,可以有三次机会。

 1 count = 0
 2 user =  'tom'#字符串
 3 password =   '22'
 4 
 5 while count < 3 :
 6     user1 = input('please enter your name:')
 7     password1 = input('please enter your password:')
 8     if user1 == user and password1 == password :
 9         print("welcome, user")
10         break
11     else:
12         print('输入错误,你一共有三次机会输入')
13     count += 1
14 print('程序退出')
View Code

算数运算符arithmetic operator:

+ - * / %

% : 取余,取模。取的是第一个操作数和第二个操作数除法的余数。整除结果为0.

10 % 3   1

10 % 5   0

10 % -3  -1

10 % -5 ?

-10%3    ?

% 真正操作步骤:

  1. 用第一个数除以第二个数,得到最相近的两个商。取最小的数。
  2. 用第一个数减去第二个数和第一步的到的数的乘积。

赋值运算符assignment operator:

基本的赋值运算符:=


扩展的赋值运算符:


+=  -=  *=  /=  %=


例如:X += Y -> X = X + Y

比较运算符compare operator:


<= >= == !=


比较运算符的结果始终是布尔类型。

逻辑运算符logic operator:

python中只有三个逻辑运算符:


and or not

逻辑运算符的结果到底是什么类型???


结果取决于两个操作数的类型!!!


针对and操作:第一个操作数如果是可以转成False的话,那么第一个操作数的值,就是整个逻辑表达式的值。


如果第一个操作数可以转成True,第二个操作数的值就是整个表达式的值。


针对or操作:第一个操作数如果是可以转成False的话,第二个操作数的值就是整个表达式的值。


如果第一个操作数可以转成True, 第一个操作数的值,就是整个逻辑表达式的值。


 




 


成员运算符:

in    not in

身份运算符:

is   not  is    (判断内存地址是否一样)


身份运算符

格式化输出:

 第一种:占位符%  %d,%s,%i,%t


 1 name = input('please input your name:')
 2 age = input('please input your age:')
 3 sex = input('please input your sex:')
 4 hobby = input('please input your hobby:')
 5 msg = '''----- info of %s -----
 6 Name  : %s
 7 Age   : %s
 8 Sex   : %s
 9 Hobby :%s
10 ------      end   ------'''%(name,name,age,sex,hobby)
11 print(msg)


1 msg = '我叫%s,我今年%s岁,我的学习进度是0.5%%' % ('tom',22)
2 print(msg)

第二种:format

①:


1 s = '我叫{},今年{},性别{}'
2 s11 = s.format('美龙', '23', 'man')
3 print(s11)


1 s = '我叫{0},今年{1},性别{2},我依然叫{0}{0}{0}'
2 s11 = s.format('美龙', '23', 'man')
3 print(s11)


s = '我叫{name},今年{age},性别{sex},我依然叫{name}'
s11 = s.format(name='美龙', sex='man', age='23')
print(s11)

第三种:


1 temp = "tom"
2 str1 = f"i am {'Zc'}"
3 str2 = f"i am {temp}"
4 print(str1)
5 print(str2)

结果


i am Zc
i am tom

原文地址:https://www.cnblogs.com/chitangyuanlai/p/9656139.html