学习python已经有一段时间了,虽然会的不是太多,但也算有了一个好的开头,下面我把我所学的python基础总结一下。
文章目录
变量和简单数据类型
1.输出Hello World
这大概是所有初学者第一个学会的输出命令
>>> print("Hello World")
Hello World
>>>
2.使用变量
>>> message="Hello World"
>>> print(message)
Hello World
注意变量名只能包含数字、字母和下划线,且只能用字母或下划线开头
变量名不能包含空格,但可使用下划线来分隔其中单词
不能将python关键字和函数名作为变量名
变量名应该既简短又具有描述性
慎重使用小写字母l和大写字母O,因为它们可能被人错看成数字1和0
3.字符串
在python中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号
>>> print("Hello World")
Hello World
>>> print('Hello World')
Hello World
>>>
3.1修改字符串大小写
首字母大写
>>> name='stefan'
>>> print(name.title())
Stefan
>>> name='stefan white'
>>> print(name.title())
Stefan White
>>>
全为大写、全为小写
>>> name='Stefan White'
>>> print(name.upper())
STEFAN WHITE
>>> name='STEFAN WHITE'
>>> print(name.lower())
stefan white
>>>
3.2合并字符串
>>> first_str="my name is"
>>> last_str="stefan,and i like python"
>>> full_str=first_str+" "+last_str
>>> print(full_str)
my name is stefan,and i like python
>>>
3.3使用制表符或换行符添加空白
>>> print('languages:\npython\nc\nphp\njava')
languages:
python
c
php
java
>>>
3.4删除空白
分别删除字符串末尾、开头、两端的空白
>>> str=' python '
>>> str.rstrip()
' python'
>>> str.lstrip()
'python '
>>> str.strip()
'python'
>>>
这种删除是暂时的,要想永久删除如下
>>> str=' python '
>>> str=str.rstrip()
>>> str
' python'
>>>
如果字符串中有撇号 ‘ 的话,应使用双引号将字符串括起来
>>> str="i'm a student"
>>> print(str)
i'm a student
>>>
4.数字
在终端会话和IDLE中,python直接返回运算结果
>>> 1+2
3
>>> 3-2
1
>>> 5*8
40
>>> 40*8
320
>>> 40/8
5.0
>>> 40%6
4
>>> 2*0.75
1.5
>>> 5.5/5
1.1
>>> 4.5+4.5
9.0
>>> 9.0-4.5
4.5
>>>
注意可能会出现这种情况,所有语言都存在这种情况,这是正常的
>>> 0.2+0.1
0.30000000000000004
>>> 3*0.1
0.30000000000000004
>>>
5.避免类型错误
>>> age=18
>>> message="my age is "+age
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
message="my age is "+age
TypeError: can only concatenate str (not "int") to str
>>>
这是因为age=18是整数(int)型变量,修改为如下
>>> age=18
>>> message="my age is "+str(age)
>>> print(message)
my age is 18
6.注释
>>> #print("hello")这是一行注释
'''
a=1b=2c=3
'''
print("这是多行注释")
列表
1.一个简单的列表
>>> words=['a','b','c','d']
>>> print(words)
['a', 'b', 'c', 'd']
>>>
2.访问列表元素
>>> words=['a','b','c','d']
>>> print(words)
['a', 'b', 'c', 'd']
>>> print(words[0])
a
>>> print(words[2])
c
>>>
索引是从0开始而不是从1开始
最后一个列表元素指定索引为-1,倒数第二个为-2,以此类推
>>> print(words[-1])
d
>>> print(words[-2])
c
>>>
3.使用列表中的值
>>> words=['a','b','c','d']
>>> print("The first word is "+words[0])
The first word is a
>>>
4.修改列表中的元素
>>> words=['a','b','c','d']
>>> words[0]='xwk'
>>> print(words)
['xwk', 'b', 'c', 'd']
>>>
5.列表中添加元素
>>> words=['a','b','c','d']
>>> words.append('e')
>>> words.append('f')
>>> words.append('g')
>>> print(words)
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>>
6.列表中插入元素
使用insert()方法可在列表任意位置添加元素
>>> words=['a','b','c','d']
>>> words.insert(0,'x')
>>> words.insert(2,'y')
>>> words.insert(4,'z')
>>> print(words)
['x', 'a', 'y', 'b', 'z', 'c', 'd']
>>>
7.列表中删除元素
7.1del删除
del可以删除任何位置的元素,前提是你知道它的索引
>>> words=['x', 'a', 'y', 'b', 'z', 'c', 'd']
>>> del words[0]
>>> words
['a', 'y', 'b', 'z', 'c', 'd']
>>> del words[1]
>>> words
['a', 'b', 'z', 'c', 'd']
>>> del words[2]
>>> words
['a', 'b', 'c', 'd']
>>>
7.2pop()删除
pop()删除列表末尾元素
>>> words=['a', 'b', 'c', 'd']
>>> pop_words=words.pop()
>>> words
['a', 'b', 'c']
>>> pop_words
'd'
>>>
显然,使用pop()删除元素,你可以得到被删除元素的值
你也可以使用它删除指定位置的元素
>>> words=['a', 'b', 'c', 'd']
>>> pop_words=words.pop(2)
>>> words
['a', 'b', 'd']
>>> print("the popped word is "+pop_words)
the popped word is c
>>>
7.3remove()删除
如果你不知道要删除元素的索引,但知道它的值,你可以使用remove()删除
>>> words=['a', 'b', 'c', 'd']
>>> words.remove('b')
>>> words
['a', 'c', 'd']
>>>
8.对列表排序
8.1按字母顺序排序
>>> name=['stefan','jack','tom','demon','lily']
>>> name.sort()
>>> print(name)
['demon', 'jack', 'lily', 'stefan', 'tom']
>>>
8.2按与字母顺序相反顺序排序
>>> name=['stefan','jack','tom','demon','lily']
>>> name.sort(reverse=True)
>>> print(name)
['tom', 'stefan', 'lily', 'jack', 'demon']
>>>
以上两种方法永久改变列表顺序
8.3临时排序
>>> name=['stefan','jack','tom','demon','lily']
>>> print(sorted(name))
['demon', 'jack', 'lily', 'stefan', 'tom']
>>> print(name)
['stefan', 'jack', 'tom', 'demon', 'lily']
>>>
列表顺序暂时改变
8.4倒序打印列表
>>> name=['stefan','jack','tom','demon','lily']
>>> name.reverse()
>>> print(name)
['lily', 'demon', 'tom', 'jack', 'stefan']
>>>
9.确定列表长度
>>> name=['stefan','jack','tom','demon','lily']
>>> len(name)
5
>>> words=['x', 'a', 'y', 'b', 'z', 'c', 'd']
>>> len(words)
7
>>>
10.列表切片
>>> name=['stefan','jack','tom','demon','lily']
>>> name[0:4]
['stefan', 'jack', 'tom', 'demon']
>>> name[0:2]
['stefan', 'jack']
>>> name[0:]
['stefan', 'jack', 'tom', 'demon', 'lily']
>>> name[:-1]
['stefan', 'jack', 'tom', 'demon']
>>> name[-3:-1]
['tom', 'demon']
>>>
循环
for循环
for i in range(1,5):
print(i)
================================
1
2
3
4
>>>
words=['a','b','c','d']
for word in words:
print("the word is "+word)
================================================
the word is a
the word is b
the word is c
the word is d
>>>
while循环
sum=0
i=1
while i<=100:
sum=sum+i
i=i+1
print(sum)
================================
5050
if判断语句
a=1
b=1
c=2
str1='hello'
str2='world'
if a==b:
print('a=b')
if a>=c:
print('a>=c')
else:
print('a<c')
if str1!=str2:
print('str1!=str2')
else:
print('str1=str2')
================================
a=b
a<c
str1!=str2
>>>
if-elif-else结构
age=12
if age<4:
print("you need pay $0")
elif age<18:
print("you need pay $5")
else:
print("you need pay $10")
================================
you need pay $5
这篇就先写这么多,后面会继续总结更新