2019年培训了4个多月的大数据应用,几个月不应用,又生疏了很多。因此,希望能重新进行快速整理,以期达到温习的效果。
#python 基础
print('Hello World!')
Hello World!
print('*'*32)
print('你的姓名')
print('生日')
print('你最喜欢的颜色')
print('*'*32)
********************************
你的姓名
生日
你最喜欢的颜色
********************************
#输入边长
# 求正方形的面积
print('请输入正方形的面积:')
a = eval(input('输入正方形边长:'))
s = a ** 2
print(s)
请输入正方形的面积:
输入正方形边长:1
1
#猜数游戏:条件、循环和随机
#输入:准确数字Num
#输入:输入可猜测数字
#循环用while
import random
Num = random.randint(1,1000)
chance = eval(input('请输入总的尝试次数:'))
print('请输入1-1000的整数!')
print('您只有'+str(chance)+'次机会噢!')
tries = 0
print(str(Num))
while tries < chance:
num = eval(input('输入数字:'))
if num > Num:
print('对不起,太大了!')
elif num < Num:
print('对不起,太小了!')
else:
break
tries = tries + 1
if num == Num:
print('恭喜您,答对了!!')
else:
print('对不起,本轮尝试失败!')
print('正确的数字为:'+str(Num))
请输入总的尝试次数:2
请输入1-1000的整数!
您只有2次机会噢!
45
输入数字:1
对不起,太小了!
输入数字:45
恭喜您,答对了!!
#if-elif-else
# for 循环
for i in range(10):
print(i)
print('*' * 32)
for i in range(1,10):
print(i)
print('*' * 32)
for i in range(10,0,-1):
print(i)
print('*' * 32)
for i in range(5,10):
print(i)
0
1
2
3
4
5
6
7
8
9
********************************
1
2
3
4
5
6
7
8
9
********************************
10
9
8
7
6
5
4
3
2
1
********************************
5
6
7
8
9
#随机数序列
import random
a1 = random.random() #0,1的随机数
a2 = random.randint(1,10) #1~10的证书
a3 = 10 * random.random() #0~10的浮点数
print(a1,a2,a3)
0.36950646596192005 9 5.8411461256684145
#每条语句以换行结束
print('Hello World!')
# 句子拆解
print('Hello W\
orld!')
Hello World!
Hello World!
'''
字符串相关函数
'''
print('hello world!'.capitalize()) #首字母大写
print(' hello world! '.strip()) #去掉左右两端空格
print(' hello world! '.lstrip()) #去掉左两端空格
print(' hello world! '.rstrip()) #去掉左右两端空格
print('*' * 32)
print('两端去除其它符号')
print('**hello ** world**'.strip('**'))
print('**hello ** world**'.lstrip('**'))
print('**hello ** world**'.rstrip('**'))
print('*' * 32)
print(dir('hello')) #字符串的属性
print('hello'+'world')
print('hello','world')
print('hello','world',sep = ',')
print('*' * 32)
print('hello')
print('world')
print('*' * 32)
print('hello',end = '**')
print('world')
print('*' * 32)
# 用join函数进行连接
a = ['hello','world','!!!']
marker = '**'
print(marker.join(a))
Hello world!
hello world!
hello world!
hello world!
********************************
两端去除其它符号
hello ** world
hello ** world**
**hello ** world
********************************
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
helloworld
hello world
hello,world
********************************
hello
world
********************************
hello**world
********************************
hello**world**!!!
#format格式化输出
print('我叫{0} , 今年{1}岁了,我喜欢学习{2}。'.format('小红', 28, 'Python'))
我叫小红 , 今年28岁了,我喜欢学习Python。
# List(列表)、Tuple(元组)、 Sets(集合)、Dictionary(字典)。
#数字类型:int、float、complex
'''
标识符命名规范:
(1)可以由字母、数字、下划线组成;
(2)标识符长度不限;
(3)必须由字母和下划线开始;
(4)大小写敏感(不同);
(5)不可以使用Python的关键字。
'''
#常量用全大写表示
a,b = 1,2
print('a = ',a)
print('b = ',b)
print('*' * 32)
a = '10'
s = int(a) + 10 #int转换为整型,float转换为浮点型
s1 = float(a) + 10
print(s,s1)
a = 1
b = 2
********************************
20 20.0
# 两种不同的方式,导入
import random
a = random.random()
print(a)
from random import random
b = random()
print(b)
0.3567803339804442
0.30804941758742477
#函数功能模块
'''
1、参数:包括形参和实参。形参有带默认值,带默认值放在最后面。
2、形参可传入不定长变量,用*T表示。应用时就为T[0]、T[1]、……T[n]
2、在声明函数形参时,先声明没有默认值的形参,然后再声明有默认值的形参。
3、全局变量、局部变量、闭包
'''
# name = 'cat'
def SayHello(name,times = 2): #默认第2次
'''
向谁打招呼
'''
hellostr = (' hello ' + name) * times
print(hellostr)
return hellostr
SayHello('cat') #默认2次
SayHello('dog',3)
print(SayHello.__doc__) #注释文档
hello cat hello cat
hello dog hello dog hello dog
向谁打招呼
#匿名函数
c = lambda a,b:a + b
print(c(1,1))
print('*' * 24)
HelloNorm = lambda name,times:print((' Hello ' + name) * times)
HelloNorm('cat',2)
2
************************
Hello cat Hello cat
'''
支持的运算符:
算术运算符、关系运算符
逻辑运算符、赋值运算符
位运算符、成员运算符、身份运算符
%:取余;//:整除,取整。
'''
'\n运算符的优先顺序:\n算术运算符、关系运算符\n逻辑运算符、赋值运算符\n位运算符、成员运算符、身份运算符\n'
55 in lst6
# 列表应用
lst1 = []
lst2 = ['hello','world',1,2] #不同类型的数据可以放在一起
lst3 = [1,2,4,6]
print('*' * 22 )
print('求列表的长度')
print(len(lst1)) #求长度1
print(len(lst2)) #求长度2
print('*' * 22 )
print('列表长度延伸')
lst1.append('haha')
lst2.extend([3,4])
lst3.insert(2,33)
#列表遍历
for i,val in enumerate(lst1):
print('{}:{}'.format(i,val))
for val in lst2:
print(val,end=' ')
for val in lst3:
print(val)
print('*' * 22 )
print(lst1+lst2)
print([lst1,lst2]) #列表嵌套
print(lst1 * 3) #列表重复多次
print('*' * 22 )
print('列表切片')
print(lst2[0]) #截取
print(lst2[-1]) #取最后一个元素
print(lst2[::-1]) #倒序
print(lst2[1:5])
print('lst2:',lst2)
lst2[0] = 'HELLO'
print('lst2:',lst2)
del lst2[0]
print('lst2:',lst2)
lst1.clear()
print('lst1:',lst1)
del lst1
print('*' * 22 )
print('列表方法')
print(lst2.count('world')) #world在列表中出现的次数
print(lst2.reverse()) #倒序,会改变lst2的值
lst5 = lst2.copy() #排序
print(lst5)
# lst3 = lst3.sort()
lst6 = [6,5,9,2,55]
print(lst6.sort())
print('lst6:',lst6)
print('*' * 22 )
print('列表函数')
print(len(lst6))
print(min(lst6))
print(max(lst6))
lst7 = (1,2,4,6) #元组
print(list(lst7)) #转换为列表
print('*' * 22 )
print('判断元素是否在列表中')
print(55 in lst6)
**********************
求列表的长度
0
4
**********************
列表长度延伸
0:haha
hello world 1 2 3 4 1
2
33
4
6
**********************
['haha', 'hello', 'world', 1, 2, 3, 4]
[['haha'], ['hello', 'world', 1, 2, 3, 4]]
['haha', 'haha', 'haha']
**********************
列表切片
hello
4
[4, 3, 2, 1, 'world', 'hello']
['world', 1, 2, 3]
lst2: ['hello', 'world', 1, 2, 3, 4]
lst2: ['HELLO', 'world', 1, 2, 3, 4]
lst2: ['world', 1, 2, 3, 4]
lst1: []
**********************
列表方法
1
None
[4, 3, 2, 1, 'world']
None
lst6: [2, 5, 6, 9, 55]
**********************
列表函数
5
2
55
[1, 2, 4, 6]
**********************
判断元素是否在列表中
True
,tup
# 元组元素不能修改,不能删除
# 要删除,只能整个元组删除
# 元组用()表示
tup1 = (50)
print(type(tup1))
tup2 = (50,) #单个元素的元组的表示方法
print(type(tup2))
tup3 = (1,2,3)
tup4 = ('hello','world')
tup5 = tup3 + tup4
del tup2 #删除元组
# 切片等与列表类似
print(('hello') * 4) #字符串多次重复
print(('hello',) * 4) #元组多次重复
#列表转成元组
lst = [1,2,4]
tup = tuple(lst)
print('tup:',tup)
<class 'int'>
<class 'tuple'>
hellohellohellohello
('hello', 'hello', 'hello', 'hello')
tup: (1, 2, 4)
#推导式
print('*****列表推导式******')
lst1 = [1,2,4,5,44]
lst2 = [i * 2 for i in lst1]
print(lst2)
lst3 = [i ** 2 for i in lst1 if i < 10]
print(lst3)
print('*****元组推导式******')
tup1 = tuple(lst3)
for val in (i+1 for i in tup1):
print(val,end=' ')
*****列表推导式******
[2, 4, 8, 10, 88]
[1, 4, 16, 25]
*****元组推导式******
2 5 17 26