Home>Article>Backend Development> What are the basic knowledge of python

What are the basic knowledge of python

PHPz
PHPz Original
2020-09-25 15:58:48 36804browse

Basic knowledge of Python: 1. References and objects; 2. Mutable data types and immutable data types; 3. Reference passing and value passing; 4. Deep copy and shallow copy; 5. Basic data types; 6. Keywords, identifiers and built-in functions; 7. Arithmetic, assignment operators, etc.

What are the basic knowledge of python

Related learning recommendations:python tutorial

Basic knowledge of Python:

1. Two ways to execute a script

Python a.py Directly call the Python interpreter to execute the file

chomd x a.py ./a.py #Modify the attributes of the a.py file to be executable, and use ./ to execute a.py File

2. Briefly describe the relationship between bits and bytes

##1bytes=8bit, 2**8=256, OK Represents the changes in 256,

3. Briefly describe the relationship between ascii, unicode, utf-8, and gbk

ascii is the earliest standard information interchange code used in the United States. It represents all uppercase and lowercase letters and various symbols in binary. There are 256 in total, with some Latin and other characters added. 1byte represents one character,

Unicode is to unify the different languages of the world. It uses 2 bytes to represent a character, which can express 2**16=65556. It is called a universal language. Features: fast, but a waste of space,

can be used in memory processing, compatible with utf-8, gbk, ASCII,

utf-8 In order to change this shortcoming of Unicode, It is stipulated that 1 English character is represented by 1 byte, and 1 Chinese character is represented by 3 bytes. Characteristics: saving space and slow speed. It is used in hard disk data transmission and network data transmission. Compared with hard disk and network speed, the performance is inferior. What comes out,

gbk is the Chinese character encoding, using 2 bytes to represent a character,

4 , please write the number of digits occupied by "Li Jie" using utf-8 and gbk encoding respectively

##utf-8 is 3 bytes=24bit

gbk is 2bytes=16bit

5. What are the uses for single-line comments and multi-line comments in Pyhton?

Single-line comments

#Multi-line comments """ """Three double quotes or three single quotes put the content to be explained in the middle, ''' '''

6. What are the precautions for declaring variables?

Universal error avoidance rules, name starting with a lowercase letter, you can Adding underscores or numbers,

cannot be named after Python keywords,

can indicate the meaning of the variable

8. How to check the address of a variable in memory?

z=

9. When executing a Python program, automatically What is the function of the generated .pyc file?

.pyc is a binary file after compiling the py file, which is used to speed up the operation

10. Write code

a. Allow the user to enter the user name and password. When the user name is seven and the password is 123, the login is successful, otherwise the login fails!
name=

b. Implement the user's input of user name and password. When the user name is seven and the password is 123, the login is successful. Otherwise, the login fails. In case of failure, the user is allowed to enter the user name and password three times.

while循环: count=
for count in range(3) : name = input('name:').strip() pwd = input('pwd:').strip()if name=='seven' and pwd=='123' :print('ok')else:print('error') count += 1

c. Implement the user's input of user name and password. When the user name is seven or alex and the password is 123, the login is successful. Otherwise, the login fails. In case of failure, the user is allowed to re-enter three times.

while 循环 count =
for count in range(3) : name = input('name: ') pwd = input('pwd: ')if name == 'seven' and pwd == '123' or name == 'alex' and pwd == '123':print('ok')else :print('error') count+=1

11. Write code

a. Use a while loop to output the sum of 2-3 4-5 6... 100
count=
num=0count=1for count in range(100) :if count % 2 == 0 : num=num - countelse : num=num+countprint(num)

c. Use a while loop to output 1, 2, 3, 4, 5, 7, 8, 9, 11, 12

count=

d. 使用 while 循环实现输出 1-100 内的所有奇数

for i in range(101) :if i %2 != 0 :print(i)
count=

e. 使用 while 循环实现输出 1-100 内的所有偶数

for i in range(100) :if i %2 == 0:print(i)
count=

12、分别书写数字 5,10,32,7 的二进制表示

print(
print(

14、现有如下两个变量,请简述 n1 和 n2 是什么关系? n1 = 123 n2 = 123

n1,n2是连个不同的变量名,但值都一样,都指向了同一个内存地址,

16、现有如下两个变量,请简述 n1 和 n2 是什么关系? n1 = 123456 n2 = n1

n2赋值给n1.指向同一个内存地址,

17、如有一下变量 n1 = 5,请使用 int 的提供的方法,得到该变量最少可以用多少个二进制位表示?

n1=5
print(int.bit_length(n1)) 结果 3

18、布尔值分别有什么?

False .Ture ,或1 和0

20、写代码,有如下变量,请按照要求实现每个功能 name = " aleX"
a. 移除 name 变量对应的值两边的空格,并输入移除有的内容

print(name.strip())

b. 判断 name 变量对应的值是否以 "al" 开头,并输出结果

print(name.startswith(

c. 判断 name 变量对应的值是否以 "X" 结尾,并输出结果

print(name.endswith(

d. 将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果

print(name.replace(

e. 将 name 变量对应的值根据 “l” 分割,并输出结果。

print(name.split(

g. 将 name 变量对应的值变大写,并输出结果

print(name.upper()) print(name.lower())

i. 请输出 name 变量对应的值的第 2 个字符?

print(name[1

j. 请输出 name 变量对应的值的前 3 个字符?

print(name[:3])

k. 请输出 name 变量对应的值的后 2 个字符?

l. 请输出 name 变量对应的值中 “e” 所在索引位置?

print(name.index(

21、字符串是否可迭代?如可以请使用 for 循环每一个元素?

依赖索引 name =
for i in name:print(i)

22、请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li = ['alex', 'eric', 'rain']

print(

22、写代码,有如下列表,按照要求实现每一个功能 li = ['alex', 'eric', 'rain']

li = [

b. 列表中追加元素 “seven”,并输出添加后的列表

li.append(
['alex', 'eric', 'rain', 'seven']

c. 请在列表的第 1 个位置插入元素 “Tony”,并输出添加后的列表

li.insert(

d. 请修改列表第 2 个位置的元素为 “Kelly”,并输出修改后的列表

li[

e. 请删除列表中的元素 “eric”,并输出修改后的列表

li = [ ['alex', 'rain'] 指名道姓的删除
li = [

f. 请删除列表中的第 2 个元素,并输出删除的元素的值和删除元素后的列表

li = [ eric ['alex', 'rain']

h. 请删除列表中的第 2 至 4 个元素,并输出删除元素后的列表

li = [ 'alex', 'eric', 'rain'] del li[ 1:] print(li)

k. 请使用 enumrate 输出列表元素和序号(序号从 100 开始)

li = [

23、写代码,有如下列表,请按照功能要求实现每一个功能 li = ["hello", 'seven', ["mon", ["h", "kelly"], 'all'], 123, 446]
a. 请输出 “Kelly”

li = [
li[

24、写代码,有如下元组,按照要求实现每一个功能 tu = ('alex', 'eric', 'rain')
a. 计算元组长度并输出

tu = (

b. 获取元组的第 2 个元素,并输出

print(tu[

e. 请使用 for、len、range 输出元组的索引

for i in range(len(tu)) :print(tu.index(tu[i]))

f. 请使用 enumrate 输出元祖元素和序号(序号从 10 开始)

10 alex 11 eric 12 rain
for k in enumerate(tu,10) :print(k)(10, 'alex')(11, 'eric')(12, 'rain')25、有如下变量,请实现要求的功能tu = ("alex", [11, 22, {"k1": 'v1', "k2": ["age", "name"], "k3": (11,22,33)}, 44])

c. 请问 tu 变量中的"k2"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”

元组,不可以

d. 请问 tu 变量中的"k3"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”

列表,可以 tu = (

26、字典
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}

for i in dic:print(i)

b. 请循环输出所有的 value

for i in dic:print(dic[i])

c. 请循环输出所有的 key 和 value

for i in dic: print(i,dic[i])

d. 请在字典中添加一个键值对,"k4": "v4",输出添加后的字典

dic[

e. 请在修改字典中 “k1” 对应的值为 “alex”,输出修改后的字典

dic[

f. 请在 k3 对应的值中追加一个元素 44,输出修改后的字典

dic[

g. 请在 k3 对应的值的第 1 个位置插入个元素 18,输出修改后的字典

dic[

27、转换
a. 将字符串 s = "alex" 转换成列表

s =

b. 将字符串 s = "alex" 转换成元祖

s =

b. 将列表 li = ["alex", "seven"] 转换成元组

li = [

c. 将元祖 tu = ('Alex', "seven") 转换成列表

tu = (
d. 将列表 li = ["alex", "seven"] 转换成字典且字典的 key 按照 10 开始向后递增dic={}for k,v in enumerate(li,10) :----li要放在前面,序号放在后面dic[k]=vprint(dic)

27、转码n = "老男孩"
a. 将字符串转换成 utf-8 编码的字节,并输出,然后将该字节再转换成 utf-8 编码字符串,再输出

n = " 老男孩 " a=n.encode( 'utf-8') print(a) b=a.decode( 'utf-8') print(b)

b. 将字符串转换成 gbk 编码的字节,并输出,然后将该字节再转换成 gbk 编码字符串,再输出

a=n.encode(

28、求 1-100 内的所有数的和

count=
count=

29、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90],将所有大于 66 的值保存至字典的第一个 key 中,
将小于 66 的值保存至第二个 key 的值中。
即: {'k1': 大于 66 的所有值, 'k2': 小于 66 的所有值}

li=[

The above is the detailed content of What are the basic knowledge of python. 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