[Python]从零开始学python——Day01~02 基础知识
2017-10-03 00:00
507 查看
国庆八天的休假说长不短,说短不长,趁着宅在家中的时间,打算从0开始学python。
与之相比,java中定义变量就得定义数据类型
还有不同的是,python中有复数的定义,java中就没有
查看变量的类型
关键字可以用keyword查看。
输出是 print()。
python的输出和C++有点类似
需要注意的是 / 返回的类型是根据参数而定的,如果除数被除数都是整型返回的就是整型,如果有浮点型,那返回的就是浮点型。
与java不同的地方有**代表幂运算,//代表结果取整
python中没有else if的写法,elif替代
for...else表示for循环正常结束后(即不是break跳出),执行else代码块
常用的就%s,%d,%f
find:从左边开始找
rfind:从右边开始找
index:从左边开始找
rindex:从右边开始找
rjust:返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
center:返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
rstrip:删除右边的空格
partition:从左边开始
rpartition:从右边开始
remove:根据内容删除
pop:删除最后一个元素(最新添加的元素)
1.基础
1.1 变量
python变量定义比较简单,不需要定义数据类型,直接写,就可以赋值num=1 str="str"
与之相比,java中定义变量就得定义数据类型
int num = 1; String str = "str";
还有不同的是,python中有复数的定义,java中就没有
查看变量的类型
print(type("i")) #<class 'str'>
1.2 标识符关键字
数字,字母,下划线组成,区分大小写,数字不能开头。关键字可以用keyword查看。
import keyword keywordList = keyword.kwlist print(keywordList)
1.3 输入与输出
python2 里面输入是raw_input(),而python3里面是input()。输出是 print()。
python的输出和C++有点类似
str =input("请输入内容") print("输入的内容%s长度是%d"%(str,len(str)))
1.4 运算符
+,-,*,/,%。需要注意的是 / 返回的类型是根据参数而定的,如果除数被除数都是整型返回的就是整型,如果有浮点型,那返回的就是浮点型。
#round(num,取值位数) print(round(2.5/2,1)) #1.2
与java不同的地方有**代表幂运算,//代表结果取整
#幂运算 print(2**10)#1K print(2**20)#1M print(2**30)#1G
2.判断语句和循环语句
2.1 if判断
与java的花括号不同,python的if格式是用缩进来判断,if判断成功后,执行缩进中的内容python中没有else if的写法,elif替代
inputNum =int(input("please input any number")) if inputNum >=0 and inputNum <10: print("0 <= num < 10,num=%d"%inputNum) elif inputNum >=10 and inputNum <20: print("10 <= num < 20,num=%d"%inputNum) else: print("%d"%inputNum)
2.2 while循环
满足条件不停循环,和java没什么区别2.3 for循环
for有点像java的foreach循环,但for不仅支持列表,也支持字符串''' 输出: a b c d e ''' str = "abcde" for i in str: print(i)
for...else表示for循环正常结束后(即不是break跳出),执行else代码块
for num in range(10,20): # 迭代 10 到 20 之间的数字 for i in range(2,num): # 根据因子迭代 if num%i == 0: # 确定第一个因子 j=num/i # 计算第二个因子 print('%d 等于 %d * %d' % (num,i,j)) break # 跳出当前循环 else: # 循环的 else 部分 print (num, '是一个质数')
2.4 continue与break
不支持break/continue a的语法,只能break/continue最近一层循环2.5 应用
写了个猜拳小游戏练习了下基础import random while True: player = input("请输入 剪刀(0) 石头(1) 布(2),结束游戏请输入end") if(player == "end"): break if(not player.isnumeric()): print("请输入数字") continue else: player = int(player) if(player <0 or player > 2): print("请输入0,1,2") continue system = random.randint(0,2) if player - system == 1 or player - system == -2: print("获胜") elif player == system: print("平局") else: print("输了")
3.函数,字符串,列表
3.1 简单函数
函数定义#分割线 def line(): print("-"*12+"华丽的分割线"+"-"*12)
3.1.1 时间函数
import time # 引入time模块 currentTime = time.time() #返回从1970.1.1开始至今的秒数 nowTime = time.asctime(time.localtime(currentTime)) print("当前时间戳为:", currentTime) print("本地时间为:",nowTime)
3.1.2 日历
import calendar cal = calendar.month(2017, 10) print("2017年10月份的日历:") print(cal)
3.1.3 随机函数
import random for i in range(0,10): print(random.randint(0,100))
3.2 字符串
python对字符串操作有一套现成的方法,操作起来异常简单3.2.1 取索引
str = "hello world" print(str[2:-1]) #从下标2的地方取到倒数第一位。'llo worl' print(str[2:]) #从下标2的地方取到最后一位。'llo world'
3.2.2 格式化
和c++的类似常用的就%s,%d,%f
3.2.3 find & rfind
检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1find:从左边开始找
rfind:从右边开始找
str = "hello world" print(str.find('world', 0, len(str))) #6
3.2.4 index & rindex
跟find()方法一样,只不过找不到,会报异常.index:从左边开始找
rindex:从右边开始找
str = "hello world" print(str.index('world', 0, len(str))) #6
3.2.4 count
返回出现的次数str = "hello world" print(str.count('world', 0, len(str))) #1
3.2.4 replace
替换内容,如果 count 指定,则替换不超过 count 次.str = "hello world" print(str.replace('world', 'python', str.count('world'))) #hello python
3.2.4 split
切割返回列表str = "hello world" print(str.split(' ')) #['hello', 'world']
3.2.5 capitalize
第一个字母大写str = "hello world" print(str.capitalize()) #Hello world
3.2.6 startswith & endswith
判断是否以什么开头,以什么结尾str = "hello world" print(str.startswith("h")) #True print(str.endswith("d")) #True
3.2.7 lower & upper
字符串全部转为小写,全部转为大写3.2.8 ljust & rjust & center
ljust:返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串rjust:返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
center:返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
str = "hello world" print("|"+str.ljust(20)+"|") #|hello world | print("|"+str.rjust(20)+"|") #| hello world| print("|"+str.center(20)+"|") #| hello world |
3.2.9 lstrip & rstrip
lstrip:删除左边的空格rstrip:删除右边的空格
str = " hello world " print("|"+str.lstrip()+"|") #|hello world | print("|"+str.rstrip()+"|") #| hello world|
3.2.10 partition & rpartition
分割成三部分,str前,str和str后partition:从左边开始
rpartition:从右边开始
str = "hello world" print(str.partition('l')) #('he', 'l', 'lo world') print(str.rpartition('l')) #('hello wor', 'l', 'd')
3.2.11 splitlines
按照行分隔,返回一个包含各行作为元素的列表str = "hell\no wor\nld" print(str.splitlines()) #['hell', 'o wor', 'ld']
3.2.12 isalnum
如果字符都是字母或数字则返回 True,否则返回 False3.2.13 isalpha
如果字符只包含字母则返回 True,否则返回 False3.2.14 isdigit
如果字符只包含数字则返回 True,否则返回 False3.2.15 isspace
如果字符只包含空格,则返回 True,否则返回 False.3.2.16 isupper
如果字符都是大写,则返回 True,否则返回 False.3.2.17 islower
如果字符都是小写,则返回 True,否则返回 False.3.2.18 join
每个字符后面插入,构造出一个新的字符串str = "hello world" print("*".join(str)) #h*e*l*l*o* *w*o*r*l*d
3.3 列表
列表支持不同数据类型在同一个列表中,java一个数组只支持一种数据类型3.3.1 索引
''' 输出 xiaoWang xiaoZhang xiaoHua ''' A = ['xiaoWang','xiaoZhang','xiaoHua'] print(A[0]) print(A[1]) print(A[2]
3.3.2 for循环
''' 输出 xiaoWang xiaoZhang xiaoHua ''' A = ['xiaoWang','xiaoZhang','xiaoHua'] for tempName in A: print(tempName)
3.3.3 增:append
通过append可以向列表添加元素,添加到最后位置A = ['xiaoWang','xiaoZhang','xiaoHua'] A.append("xiaoMing")
3.3.4 改:=
通过下标来赋值A = ['xiaoWang','xiaoZhang','xiaoHua'] A[0] = "xiaoMing"
3.3.5 查: in / not in
A = ['xiaoWang','xiaoZhang','xiaoHua'] if 'xiaoWang' in A: print("find")
3.3.6 删:del / remove / pop
del:根据下表删除remove:根据内容删除
pop:删除最后一个元素(最新添加的元素)
相关文章推荐
- java基础 02 从零开始,基础知识
- 兄弟连学python(02)——python 基础知识 列表、字典、元组、集合
- python基础知识day01
- python机器学习日志.02——Python编程的基础知识
- Swift3.0基础知识脑图02
- Java基础知识02-流程控制-do while
- python操作MongoDB基础知识
- 【转】Python基础知识
- python 基础知识补充
- Python学习笔记(一)----Python基础知识
- 01月29日【Python3 基础知识】
- Java基础知识02-增强型的foreach循环
- Python笔记-容易出错应牢记的基础知识
- python_基础学习_02_拆分文件(spilt)
- (总结)python 基础知识一
- Python - 基础知识 - 条件判断
- python学习一--基础知识
- python学习笔记-Day14 -css基础知识-part2
- week01_python基础语法__02
- Python 数据分析(一) 本实验将学习 pandas 基础,数据加载、存储与文件格式,数据规整化,绘图和可视化的知识