1基础数据类型
数字:
int 1,2,3
+ - * / %(取余)
PS:查看数据类型print(type(1))
字符串转化成数字:int(str) 条件:str必须是数字组成
数字转换字符串:str(int)
字符串:
str Python凡是用引号引起来的都是字符串.
字符串可以相加(拼接).
可以相乘. str*int
bool:布尔值
True/False
2.流程控制
if 条件:
结果
#第一种
if 4 >3:
print(333)
#第二种
if 5>3:
print(555)
else: #否则
print(333)
#第三种
name = input('请输入:')
if name == '1': //用户输入的1是int类型,所以说需要加上单引号//
print('三星')
elif name == '2':
print('荣耀')
elif name == '3':
print('mac book Por')
elif name == '3':
print('iPhone X 4G在线')
else:
print('请输入有效范围数字[1|2|3|4]')
12.while
循环体:
无限循环:
终止循环:1.改变条件
2.break
3.continue结束本次循环,继续下次循环
count = 0 #从1-100
flag = True #标志位
while flag:
print(count)
count = count +1
if count >100:
flag = False
3复习:
python2和python3的区别:
python2x源码,重复率高,不规范,而且python崇尚的是简洁优美,创建了python3,规范化
在python2首行:#-*-encoding:utf-8 -*-解决python2中文报错
变量:有数字字母下划线任意组合,且不能以数字开头,具有可描述性,不能用python中的关键在i,不能用中文及拼音
变量:约定成俗 不可更改 全部是大写在字母
注释:
单行注释:#
多行注释: ''' ''' """ """
用户交互input:
数据类型全都是str
基础数据类型:bool True False
int +-*/% // **
str: 加引号的就是str
+可以与数据*
if 条件:
结果
if 条件:
结果
else:
结果
if 条件:
结果
elif:条件
结果
elif:条件
结果
else:结果
if 条件:
if条件:结果
if...
else:结果
while 条件:
结果
1,改变条件
2.break
3.continue:结束本次循环,继续你下次循环
4练习题:
1.使用while 循环输入1 2 3 4 5 6 8 9 10
'''
count = 0
while count< 11:
count += 1
if count == 7:
continue
print(count)
'''
'''
count = 0
while count< 11:
count += 1
if count == 7:
pass #跳过
else:
print(count)
'''
2.求1-100内的和
'''
n = 1
s = 0
while n < 101:
s = s + n
n = n + 1
print(s)
'''
3.取出1-100内的奇数
count = 1
while count < 101:
print(count)
count +=2
count = 1
while count < 101:
if count % 2 == 1:
print(count)
count +=1
偶数:
'''
'''
count = 1
while count < 101:
if count % 2 == 0:
print(count)
count +=1
'''
4.求1+2+3+4...99的所有的和
sum = 0 ///count等于1的时候sum等于0+1,count=2的时候sum=0+1+2
count =1
while count <=100:
sum = sum + count
count +=1
print(sum)
5.#用户登陆三次
i = 0
while i < 3:
username = input('请输入账号:')
password = int(input('请输入密码:'))
if username == 'root' and password == 123:
print('登陆成功!')
break
else:
print('密码错误,请重新登陆')
i +=1
5.格式化输出
%s 字符串占位符
%d 数字占位符
% 如果想在格式化输出单纯的%,就在前边加上%就好了相对于转义符,
username = input('>>:')
age = int(input('>>:'))
height = int(input('>>:'))
msg = "我叫:%s.\n年龄为:%s.\n身高为:%s.\n" %(username,age,height)
print(msg)
练习题:
username = input('姓名:')
gender = input('性别:')
Nationality = input('民族:')
birth = int(input('请输入出生年:'))
b = int(input('请输入出生月:'))
a = int(input('请输入出生日:'))
address = input('请输入现住址:')
ID_card = int(input('请输入身份证:'))
msg = '''
---------info %s-----------
姓名 %s
性别 %s\t\t民族 %s
出生 %s 年%s 月%s 日
住址 %s\t\n
公民身份证 %s
-----------END---------------
'''% (username,username,gender,Nationality,birth,b,a,address,ID_card)
print(msg)
6.whlie..else
当whlie循环被break打断,就不会走下面的else的结果count = 0
while count <= 5:
count += 1
if count == 3:
break
print('loop',count)
else:
print('打印完毕')
print("--------out of while loop")
7.运算符&编码
ascii 只显示英文,特殊字符,数字
万国码 unicode最开始16位,中文不够32位 4个字节。
升级utf-8 utf-16 utf-16
utf-8:最少用一个字节,8位表示一个英文
欧洲16位,两个字节
亚洲 24位 三个字节
gbk:只能用于中文和cscii码中的文字
逻辑运算:and or not
优先级() >not >and >or
#x or y x True,则返回x
print(1 or 2)
#x and y x True,则返回y
print(1 and 2)
8 数据类型
int:123用于计算
bool:布尔值(false/true)用于用户判断
str:存储少量数据,进行操作,用引号引起来的
list(列表):存储大量的数据
元祖:只读。
dict(字典):
{'name':'jd','age':'12'}
集合:{1,2,3,4,'sasd'}
int:123用于计算 bool:布尔值(false/true)用于用户判断 str:存储少量数据,进行操作,用引号引起来的 list(列表):存储大量的数据 元祖:只读。 dict(字典): {'name':'jd','age':'12'} 集合:{1,2,3,4,'sasd'}
9转换关系:
#int ---> str
i = 1
s = str(i)
print(type(s))
#str ---> int
s = '123'
a = int(s)
print(type(a))
#int -->bool 只要是 0 --->False 非0就是True
int = 3
b = bool(i)
print(b)
#str --->bool,非空字符串都是True
s = "12" #----False
n = bool(s)
print(n)
#bool--->int
#true 1
#False 0
#ps: while 1:效率高
10str操作:
#判断元素以什么结尾
sc = 'alex'
sv = sc.endswith('ex')
print(sv)
#首字母大写
s = 'alex'
s1 = s.capitalize()
print(s1)
#全部大/小写
s2 = s.upper()
s.lower全部小写
print(s2)
#大小写反转
s3 = s.swapcase()
print(s3)
#字符串居中/前后两边填充
s = 'mycahjack'
s4 = s.center(20,'$') #字符串居中/前后两边填充
print(s4)
#计算元素有几个字符
s = 'adgfsagad'
ss = len(s) #查看有几个字符
print(ss)
#判断以什么开头false/True
s5 = s.startswith('my')
s6 = s.startswith('a',3,5) #顾头不顾尾。判断第3个元素是否以a开头
print(s5)
#通过元素找索引,找不到返回-1
s = 'alexwusir'
s1 = s.find('w') #寻找这个元素返回的是这个元素的索引
print(s1)
#方法二 首选使用find
s = 'alexwusir'
s1 = s.index('4') #找不到会报错
print(s1)
#去掉前后空格 rstrip lstrip
s = ' alexwusir'
s1 = s.strip() #也可以删除()里的元素
print(s1)
#统计有几个a
s = 'aaexfasdf'
s1 = s.count('a')
print(s1)
#字符串转换成列表
s = 'aae;ad;dsagf'
s1 = s.split(':')
print(s1)
#format的三种用法,格式化输出
1)s = '我叫{},今年{},爱好,再说一下我叫{}'.format('李',23,'girl','李')
print(s)
2)s = '我叫{0},今年{1},爱好,再说一下我叫{0}'.format('李',23,'girl')
print(s)
3)s = '我叫{name},今年{age},爱好{a},再说一下我叫{name}'.format(name='李',age=23,a='girl')
print(s)
#替换
a = '盖伦is and or not盖伦'
s2 = a.replace('盖伦','剑豪') 全替换
print(s2)
ps:想替换一个可以('盖伦','剑豪',1)
#for 循环
#str字符串索引切片
m = 'asdfasdg'
s3 = m[0]
print(s3)
#str索引/切片
#取出一个元素
m = 'asdfasdg'
s3 = m[1]
print(s3)
#取出0-4元素,顾头不顾尾
sa = m[0:4]
print(sa)
#取出最后一个元素-1
sv = m[-1]
print(sv)
#取出全部
sc = m[0:]
print(sc)
sz = m[:]
print(sz)
#跳着取/步长取s[首:尾:步长]
a = 'abcdefghijk'
s = a[0:5:3]
print(s)
#倒着取
s11 = a[4:0:1]
print(s11)
s12 = a[3::-1]
print(s12)
11复习
ascii:字母.数字.特殊字符:1个字节.8位
Unicode:16位 两个字节 升级32位 四个字节
utf-8:最少一个字节.8位表示。英文字母8位 1个字节
欧洲16位,2个字节
中文24位,3个字节
gbk:中文2个字节.英文字母1个字节
int:bit_lenth()
bool:false/ture
str:str--->bool:bool(str) --False