Home  >  Article  >  Backend Development  >  Summary of basic knowledge about python3 learning

Summary of basic knowledge about python3 learning

高洛峰
高洛峰Original
2017-03-19 14:17:1634676browse

1. Data type

1. Number

  • int (integer type)

  • long (long integer type)

  • floatfloat

  • ##complex (plural)

2. Boolean value

  • True or False

3, String

2, Variable

Variable naming rules:

三, String splicing

1. Use the plus sign (+)

name = "Tom"age = 25print(name + "s age is " + str(age))
#输出:Toms age is 25

2. String formatting

name = = 25( %
ps: In

python, use the + sign to connect strings. Every time a + sign appears, you must re-apply for a space in the memory. How many + signs are there? How much space do you need to apply for? Generally do not use the + sign to connect strings.

4. Lists and Tuples

1. List

  • Create a list

str_list = ['Tom','Lucy','Mary']
或者
str_list = list(['Tom','Lucy','Mary'])
  • Index (access a value in the list)

str_list[0]
  • Append (add elements to the end)

str_list.append('lilei')print(str_list)#输出:['Tom', 'Lucy', 'Mary', 'lilei']
  • Insert (add an element at the specified position)

str_list.insert(1,'lilei')print(str_list)#输出:['Tom', 'lilei', 'Lucy', 'Mary']
  • Delete (delete the specified element)

str_list.remove('Lucy')print(str_list)#输出:['Tom', 'Mary']
  • Slice

  • str_list = [3,4,5,6,7,8,9]
    new_1 = str_list[1:3]    #从索引1开始取,取到索引3
    new_2 = str_list[0:6:2]  #从索引0开始取,每两位一取,到第6位为止
    new_3 = str_list[-2:]    # 取后面2个数
    new_4 = str_list[:3]     # 取前面3个数
    new_5 = str_list[::3]    #所有数,每3个取一个
    
    print(new_1,new_2,new_3,new_4,new_5)
    
    #输出:[4, 5] [3, 5, 7] [8, 9] [3, 4, 5] [3, 6, 9]

2. Tuple

  • Creating tuples

age = (18,25,33)
或者
age = tuple((18,25,33))
Except that elements cannot be modified, added, or deleted, other operations on tuples and lists are almost the same.

5. Dictionary

Use key-value storage method

  • Create dictionary

  • phone = {
        '张三':'13075632152',
        '李四':'15732015632',    
        '王五':'13420321523',
    }
  • Get the value of the key in the dictionary

  • print(phone['张三'])      
    #如果key不存在,会报错,key用中括号装print(phone.get('老黄'))  
    #如果key不存在,返回None,key用小括号装#输出:13075632152
    #     None
  • Assignment

phone[] =    
phone[] =
  •  删除

phone.pop('张三')   
#第一种方法del phone['李四']   
#第二种方法phone.popitem()    
#随机删除某一个
  • 遍历

for key in phone:
    print(key,phone[key])

#输出:
# 王五 13420321523
# 张三 13075632152
# 李四 15732015632
  • 多级嵌套

phone = {
    '人事部':{'老张':'13700112233','老李':'13432023152'},
    '财务部':{'小丽':'13230555666','小映':'13723688888'},
    '技术部':{'老罗':'13866666333'}
}

print(phone['人事部']['老李'])

#输出:13432023152

六、if语句

1、if...else

age = 16
if age <18:
    print(&#39;你还未成年呢&#39;)
else:
    print(&#39;你已经成年了&#39;)

2、if...elif....else

score = 85
if score > 0 and score< 60:
    print(&#39;你的成绩不及格&#39;)
elif score >= 60 and score <80:
    print(&#39;你的成绩及格了&#39;)
elif score>=80 and score<90:
    print(&#39;你的成绩良好&#39;)
else:
    print(&#39;你的成绩优秀&#39;)

七、while循环

i=0
num=0
while i<=100:
    num+=i
    i+=1
print(&#39;1-100累加等于%d&#39;%num)

 八、for...in循环

num = []
for i in range(10):
    num.append(i)
print(num)

#输出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

九、用户交互(input)

name = input(&#39;请输入你的名字:&#39;)
height = input(&#39;请输入你的身高:&#39;)
print(&#39;%s的身高%s厘米&#39; %(name,height))

 十、文件基本操作

 打开文件:f = open('文件路径','模式')    或者   with open('文件路径','模式') as f:

模式:

  • r:以只读方式打开文件

  • w:打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。

  • a:打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。

  • w+:打开一个文件用于读写。(文件一打开就清空了,还能读到东西吗?)

  • a+:打开一个文件用于读写。

 读文件:

 read()  readlines()  readline() 的用法

f = open(&#39;d:/test.txt&#39;,&#39;r&#39;)  #以只读方式打开文件

print(f.read())  #read()一次读取文件的全部内容

for line in f.readlines():   #readlines()读取整个文件,并按行存进列表
    print(line.strip(&#39;\n&#39;))  #去掉行尾的&#39;\n&#39;

while 1:
    line = f.readline()   #readline()每次只读取一行
    print(line.strip(&#39;\n&#39;))
    if not line:
        break

f.close()   #关闭文件

写文件:

f =open(&#39;d:/test.txt&#39;,&#39;a&#39;)
f.write(&#39;hello,boy!\n&#39;)  
f.close()

The above is the detailed content of Summary of basic knowledge about python3 learning. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn