(一)基本数据类型:
str 字符串 int 整数
数字: 不加 '' (引号)默认是数字类型
int 整数 不加''默认是数字类型
type(...) 查看数据类型
python3里所有的整数都按 int 类处理
其他语言中必须在声明变量前先定义好其类型。强类型
python 弱类型
float 浮点数~小数
字符串:
定义
name = "qiangzi" #有引号就是字符串
''(单引号)""(双引号)''' ''''(三引号段落)"i'm qiangzi"
拼接
>>> s1="床前明月光"
>>> s2="疑是地上霜"
>>> print(s1+s2)
床前明月光疑是地上霜
>>> s1="我是强子。"
>>> s1*3
'我是强子。我是强子。我是强子。'
段落
name ='''
qiangzi
... xiaofang
... gouzi
... '''
print(name)
qiangzi
xiaofang
gouzi
(二)bppl 布尔类型
a=3
b=5
a<b 正确的|成立的 真 Ture
>>> a=3
>>> b=5
>>> a<b
True
a>b 错误的|不成立的 假f False
>>> a=3
>>> b=5
>>> a>b
False
猜年龄游戏:
age_of_qiangzi = 25
count = 0
while count<3:
guess_age = int(input("age_of_qiangzi:"))
if guess_age == age_of_qiangzi:
print("yes,you got it!")
break
elif guess_age < age_of_qiangzi:
print("think bigger!")
else:
print("think smaller!")
count += 1
if count == 3:
countine_confirm = input("do you want to mang times..?")
if countine_confirm != 'n':
count = 0
else:
print("GAME OVER!!")
(三)列表
#列表、数据
#元素名: qiangzi longzong xifeng xiaofang shijia
#下标(索引): 0 1 2 3 4
["",""] 列表格式
names = ["qiangzi","longzong","xifneg","xiaofang","shjia"]
#"qiangzi" 叫元素
增
插入 .insert
names = ["qiangzi","longong","xifeng","xiaofnag","shijia"]
>>> #.insert(x,"")插入
... names.insert(4,"daqiang")
>>> print(names)
['qiangzi', 'longong', 'xifeng', 'xiaofnag', 'daqiang', 'shijia']
追加 .append 元素插入在列表最后
>>> names = ["qiangzi","longong","xifeng","xiaofnag","shijia"]
>>> #.append("")追加
... names.append("xiaoqiang")
>>> print(names)
['qiangzi', 'longong', 'xifeng', 'xiaofnag', 'shijia', 'xiaoqiang']
删 del[x]
names = ["qiangzi","longong","xifeng","xiaofnag","shijia"]
>>> #del[x]删除
... del(names[1])
>>> del(names[-1])
>>> print(names)
['qiangzi', 'xifeng', 'xiaofnag']
改
>>> names = ["qiangzi","longong","xifeng","xiaofnag","shijia"]
>>> #直接赋值 names[x] = ""
... names[-1] = "事假"
>>> print(names)
['qiangzi', 'longong', 'xifeng', 'xiaofnag', '事假']
查
>>> names = ["qiangzi","longong","xifeng","xiaofnag","shijia"]
>>> print(names[0])
qiangzi
>>> #查询元素是否在列表里
... print("qiangzi" in names)
True
>>> #.index 查询列表里元素的下标(索引)
... del(names[names.index("qiangzi")]) #命令嵌套
>>> print(names)
['longong', 'xifeng', 'xiaofnag', 'shijia']
(四)读取格式化输入、入库、查询
#信息格式化输入、存库、查找 源码
names = ["longzong","liye","wangcong"]
Names = input("姓名:",)
Pressional = input("专业:")
Class_and_grade = input("班级:")
Student_number = int(input("学号:"))
msg ='''
-----------学生信息----------
姓名:%s
专业:%s
班级:%s
学号:%d
-------------end------------
''' %(Names,Pressional,Class_and_grade,Student_number)
print(msg)
names.append(Names)#追加元素
print(names)
print(Names in names)
print(names[-1])
E:\python\venv\Scripts\python.exe E:/python/匹配成绩的小游戏.py
姓名:强子
专业:电气工程及其自动化
班级:电气17-3班
学号:20170584
-----------学生信息----------
姓名:强子
专业:电气工程及其自动化
班级:电气17-3班
学号:20170584
-------------end------------
['longzong', 'liye', 'wangcong', '强子']
True
强子
Process finished with exit code 0
基本计算符
计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算,今天我们暂只学习算数运算、比较运算、逻辑运算、赋值运算
算数运算
以下假设变量:a=10,b=20
比较运算
以下假设变量:a=10,b=20
赋值运算
以下假设变量:a=10,b=20
逻辑运算
流程控制
if...else...
单分支
the weather = "randy"
if the weather == "randy":
print("带伞”)
双分支
'''_username = "qiangzi"
_password = "zj521."
username = input("username:")
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} loading...!".format(name = username))
else:
print("Inlated username and password!")
age_of_qiangzi = 25
count = 0
while count<3:
guess_age = int(input("guess_age:"))
if guess_age == age_of_qiangzi:
print("Yes,you got it!")
break
elif guess_age > age_of_qiangzi:
print("Think smaller!")
else:
print("Think bigger!")
count +=1
if count == 3:
countine_confirm = input("do you want to keep guessing...?")
if countine_confirm != "n":
count = 0
#else:
# print("you have tried too many times..fuck off")
age_of_qiangzi = 25
count = 0
while count<3:
guess_age = int(input("age_of_qiangzi:"))
if guess_age == age_of_qiangzi:
print("yes,you got it!")
break
elif guess_age < age_of_qiangzi:
print("think bigger!")
else:
print("think smaller!")
count += 1
if count == 3:
countine_confirm = input("do you want to mang times..?")
if countine_confirm != 'n':
count = 0
else:
print("GAME OVER!!")
多分支
age_of_qiangzi = 25
count = 0
while count< 3:
guess_age = int(input("age_of_qiangzi:"))
if guess_age == age_of_qiangzi:
print("yes, you got it!!")
break
elif guess_age < age_of_qiangzi:
#break
# elif guess_age < age_of_qiangzi
print("think bigger!!")
else:
# else:
print("think smaller!")
count +=1
if count == 3:
countine_confirm = input("do you want to keep guessing...?")
if countine_confirm != "n":
count = 0
else:
print("GAME OVER!")'''
#打印0到100偶数
count = 0
while count < 100:
if count % 2 == 0:
print("fuck you!",count)
count += 1