What are the basic knowledge of python
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.
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=24bitgbk 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=
.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=
Copy after login
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. name=
while循环:
count=
Copy after loginfor 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
Copy after login
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:').strip() pwd = input('pwd:').strip()if name=='seven' and pwd=='123' :print('ok')else:print('error') count += 1
while 循环
count =
Copy after loginfor 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
Copy after login
11. Write codewhile 循环 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
a. Use a while loop to output the sum of 2-3 4-5 6... 100
count=
Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login num=0count=1for count in range(100) :if count % 2 == 0 : num=num - countelse : num=num+countprint(num)
Copy after login
c. Use a while loop to output 1, 2, 3, 4, 5, 7, 8, 9, 11, 12count=
num=0count=1for count in range(100) :if count % 2 == 0 : num=num - countelse : num=num+countprint(num)
n1,n2是连个不同的变量名,但值都一样,都指向了同一个内存地址, n1=5 False .Ture ,或1 和0 元组,不可以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
16、现有如下两个变量,请简述 n1 和 n2 是什么关系? n1 = 123456 n2 = n1
n2赋值给n1.指向同一个内存地址,
17、如有一下变量 n1 = 5,请使用 int 的提供的方法,得到该变量最少可以用多少个二进制位表示?
print(int.bit_length(n1)) 结果 318、布尔值分别有什么?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
