实现hello world
- 不区分单双引号,print(“hello world!”)与print(‘hello world!’)输出结果一样
- python的print自带换行
认识加减乘除
- 两个乘法是指数运算
print(2**6)
64
- 普通除法中整数相除必得到浮点数,地板除(//)结果是整数
print(2/3)
0.6666666666666666
print(3//2)
1
- 两个字符串可以利用加号进行“拼串“
print('a'+'b')
ab
- 字符串和整数之间可以进行乘法,结果是重复几遍字符串
输入
- 变量不需要声明
- input()结果是字符串类型
- input()括号里可以加字符串用来显示
注释
- #号单行注释
#xxxx - 三对引号(单或双)多行注释
‘’‘xxx’’’
if判断
- if后面要有空格
- 空格后面是判断主体
- 结尾要有:循环体要**缩进 **
else if可以缩进成elif,python编程中else if=elif
最后一个else后面加==:==
if num==yuqi:
print('你猜对了!')#强制转换
elif num>yuqi:
print('大了')
elif num<yuqi:
print('小了!')
循环
- while,for,break,continue,pass(表示跳过)可用于循环
- 不存在C语言中i++,i–,但可以是i+=1,i=i+1
例如以下程序:
for i in range(5):
num=input('请输入0——100以内的数字:') #等同于C中for(i=0,i<5,i++)
num=int(num)#强制转换
yuqi=65
if num==yuqi:
print('你猜对了!')
elif num>yuqi:
print('大了!')
elif num<yuqi:
print('小了')
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=========================== RESTART: E:/py/2.3.py ===========================
请输入0——100以内的数字:10
小了
请输入0——100以内的数字:50
小了
请输入0——100以内的数字:60
小了
请输入0——100以内的数字:90
大了!
请输入0——100以内的数字:100
大了!
变量类型
- 整型int
- 字符型char:'a’字符串之间比较大小容易出错(见如下代码)
- 布朗型bool:True,False(T,F必须大写)计算机默认True1,False0
type(1)
<class 'int'>
>>> type(1.0)
<class 'float'>
>>> type('1')
<class 'str'>
>>> True
True
>>> type(True)
<class 'bool'>
>>> type(true)
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
type(true)
NameError: name 'true' is not defined
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> 1>5
False
>>> type(1>5)
<class 'bool'>
>>> 1<5
True
>>> '1'<'5'
True
>>> #字符串之间比较的是ASC2
>>> '11'>'55'
False
>>> '11'<'55'
True
>>> '54'='45'
SyntaxError: can't assign to literal
>>> '54'=='45'
False
>>> '54'=='145'
False
>>> '54'>'145'
True
>>> '54'=='54'
True
>>> '54'=='154'
False
>>> 1==True
True
>>> 11=True
SyntaxError: can't assign to literal
>>> 11==True
False
>>> 2==True
False
>>> 0==False
True
>>> 2==False
False
>>> type(2)
<class 'int'>
>>> len(2)
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
len(2)
TypeError: object of type 'int' has no len()
>>> len("a")
1
>>>
- 浮点型
- list型等
列表
- 例如a=[1,2,3,‘a’,True]
- 列表属于list型·类似于C语言中的一维数组
- 列表切片,逆切片
a=[1,2,3,'a',True]
>>> a
[1, 2, 3, 'a', True]
>>> a[0]
1
>>> a[4]
True
>>> type(a[4])
<class 'bool'>
>>> a[0:3]
[1, 2, 3]
>>> a[:3]
[1, 2, 3]
>>> a[3:]
['a', True]
>>> a[:-3]
[1, 2]
>>> a[-3:]
[3, 'a', True]
>>> for i in a:
print(i)
1
2
3
a
True
>>> for i in range(5):
print(i)
0
1
2
3
4
>>>
- Python列表函数&方法
Python包含以下函数:
cmp(list1, list2)比较两个列表的元素
l en(list) 列表元素个数
max(list)返回列表元素最大值
min(list)返回列表元素最小值
list(seq)将元组转换为列表
5.Python包含以下方法:
序号 | 方法 | 作用 |
---|---|---|
1 | list.append(obj) | 在列表末尾添加新的对象 |
2 | list.count(obj) | 统计某个元素在列表中出现的次数 |
3 | list.extend(seq) | 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表 |
4 | list.index(obj) | 从列表中找出某个值第一个匹配项的索引位置 |
5 | list.insert(index, obj) | 将对象插入列表 |
6 | list.pop([index=-1]) | 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 |
7 | list.remove(obj) | 移除列表中某个值的第一个匹配项 |
8 | list.reverse() | 反向列表中元素 |
9 | list.sort(cmp=None, key=None, reverse=False) | 对原列表进行排序 |
a=[0,1,'a',True,4]
>>> comp(a[0],a[1])
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
comp(a[0],a[1])
NameError: name 'comp' is not defined
>>> cmp(a,a)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
cmp(a,a)
NameError: name 'cmp' is not defined
>>> len(a)
5
>>> max(a)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
max(a)
TypeError: '>' not supported between instances of 'str' and 'int'
>>> max(a[:2])
1
>>> min(a[:2])
0
>>> b=[0]
>>> b
[0]
>>> b.append(a)
>>> b
[0, [0, 1, 'a', True, 4]]
>>> b[1]
[0, 1, 'a', True, 4]
>>> b[1][2]
'a'
>>> b[1].append(1)
>>> b
[0, [0, 1, 'a', True, 4, 1]]
>>> b.extend(a)
>>> b
[0, [0, 1, 'a', True, 4, 1], 0, 1, 'a', True, 4, 1]
>>> b.index(0)
0
>>> b.insert(0,1)
>>> b
[1, 0, [0, 1, 'a', True, 4, 1], 0, 1, 'a', True, 4, 1]
>>> b.pop(0)
1
>>> b
[0, [0, 1, 'a', True, 4, 1], 0, 1, 'a', True, 4, 1]
>>> b.remove(0)
>>> b
[[0, 1, 'a', True, 4, 1], 0, 1, 'a', True, 4, 1]
>>> b.clear()
>>> b
[]
>>>
元组
Python的元组与列表类似,不同之处在于元组的元素不能修改
- 元组使用小括号(),列表使用花括号【】
- tup1 = (‘physics’, ‘chemistry’, 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = “a”, “b”, “c”, “d”
元组中只包含一个元素时,需要在元素后面添加逗号,例如:tup1 = (50,)
一. 修改元组
元组中的元素值是不允许修改。
二. 删除元组
元素值是不允许删除的,但我们可以使用del语句来删除整个元组
三. 元组运算符
Python 表达式 | 结果 | 描述 |
---|---|---|
len((1, 2, 3)) | 3 | 计算元素个数 |
(1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 连接 |
(‘Hi!’,) * 4 | (‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’) | 复制 |
3 in (1, 2, 3) | True | 元素是否存在 |
for x in (1, 2, 3): print x, | 1 2 3 | 迭代 |
四. 列表与元组的异同
1.不同: 列表可以进行增删改查,而元组只可以查,不能增改删,否则会报错。
2.相同:
求长度表达式len()
len((1,2,3))
3
>>> len([1,2,3])
3
>>> len('123')
3
访问方式:可以使用下标索引来访问元组中的值,如下实例:
>>> [1,2,3]
[1, 2, 3]
>>> a=(1,2,3)
>>> a[0]
1
>>> a=[1,2,3]
>>> a[0]
1
>>>
两个表达式组合
(1,2,3)+(4,5,6)
(1, 2, 3, 4, 5, 6)
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
表达式与数之间可进行乘法:都为重复作用
('123',)*4
('123', '123', '123', '123')
>>> ['123']*4
['123', '123', '123', '123']
判断元素是否存在表达式
3 in (1,2,3)
True
>>> 3 in [1,2,3]
True
具有有序性:元素相同,顺序不同,表达式含义不同
a=[1,2,3]
>>> b=[3,2,1]
>>> a==b
False
a=(1,2,3)
>>> b=(3,2,1)
>>> a==b
False
都可切片且方式相同
例如:a=(1, 2, 3)
>>> a[-2:2]
(2,)
a=[1,2,3]
>>> a[-2:2]
(2,)
函数方法相同
例如:
>>> b=[1,2,3,4,5,1,2]
>>> b.count(1)
2
>>> a=(1,2,3,4,5,8,7,1,5,4,65,4)
>>> a.count(1)
2