Learn Python quickly in 7 days - Day 1
1 Environment setup and basic introduction
1.Environment setup
-
The computer can only recognize: 010101010101010010101
-
Python developer
print("hello world")
- Translator: Python interpreter (download and install)
If you want to learn Python, you essentially need to do the following three steps:
-
Successfully installed the interpreter on your computer.
-
Learn Python's syntax and write code.
print("xxx") name = "xxx" x = input("...")
-
Use the interpreter to run the code.
1.1 Install the interpreter (software)
https://python.org/
Download python and install it.
- About the download version: https://www.python.org/downloads/ (recommended: 3.9.0)
Note: The interpreter installation path should not contain a Chinese path.
1.2 Installation directory
Assuming the installation directory:C:\Python39\
C:\Python39
- python.exe 就是我们的解释器。
- Scripts
- pip.exe 帮助我们以后安装第三方包。
- Lib Python内置的源码
- 文件/文件夹 Python提供的内置功能
- site-packages 通过pip去安装第三方的包时,他就会放在这个目录。
1.3 Python interpreter
If you want to use the Python interpreter, you must do it on the terminal.
-
Interactive, not commonly used.
-
file format.
1.假设在自己的 F:\code.py 创建了一个文件。 2.在文件中写了点代码。 3.运行代码 C:\Python39\python.exe F:\code.py
1.4 Environment variables
C:\python35\python.exe C:\code\first.py
C:\python35\python.exe C:\code\second.py
C:\python35\python.exe C:\code\xxx.py
C:\user\python\python37\python.exe C:\code\xxx.py
If you put a directory in the system environment variable, for example:
将 C:\user\python\python37 放到环境变量。
In the future, when we run python code on the terminal device, we will not need to write the previous path.
python.exe C:\code\xxx.py
How to add environment variables?
-
win7
C:\Python39\;C:\Python39\Scripts\;...
-
win10
Operate together
-
Write a code using a text document
hello.py
print("Hello World")
-
Open terminal
-
run
summary
-
Do not mix the Python interpreter and your own code files in the same directory.
- Python interpreter installation path:
C:\python39\
- Own code path:
F:\code\
- Python interpreter installation path:
-
The python interpreter has two modes
- interactive
- file format
-
When running the code, if an error occurs, don’t panic
After the above learning is completed, when we write code in the future, we need to create files ourselves & run the code in the terminal.
Generally, in the real development process, manual operations are not performed.
2. IDE
IDE, integrated development environment (write code quickly and run it).
The most commonly used IDE in Python development is: Pycharm.
-
Community version, less features [free].
-
Professional version, many functions [charged] [30-day trial]
Note: Do not install Chinese plug-ins without permission.
2.1 Open for the first time
2.2 Create project (folder)
2.3 Create files
summary
At this point, we have successfully set up a Python environment.
- Interpreter environment: interpreter + file => Manually execute the code.
- Pycharm: Conveniently help me automatically
解释器+文件
=> Automatically execute.
Therefore, in order to improve development efficiency in the future, you need to use Pycharm to write code when writing code.
3.Python syntax
3.1 Encoding
-
The tool writes Chinese characters, letters, and numbers. After writing, you need to save it to the hard disk.
徐一yyds666 01010101010101101010101001010101000111101
-
A set of encodings (codebooks) UTF-8
徐 100000011100101 一 111111100000001 y 111111110000001 d 111111101000001 s 111111100010001 6 111111100000101
-
Use software to open user.txt
徐 100000011100101 一 111111100000001 y 111111110000001 d 111111101000001 s 111111100010001 6 111111100000101
yyds666
There is more than one set of codes (ciphertext) in a computer.
When writing files in the future, be sure to remember what encoding you use when saving your files? When you open this file in the future, you need to use the same encoding to open it, otherwise, garbled characters will appear.
This rule still needs to be followed during Python development:
-
Write Python code in the file (we will save the code in UTF-8 encoding later).
print("Hello World")
print(“hellow world”)
-
The python interpreter opens the code and reads the file contents, converting them into a language that the computer can recognize.
Python解释器会打开咱们的文件。 默认:Python3.x版本解释器,默认会使用utf-8编码去打开文件。 >>>C:\python39\python.exe D:\code\first.py
3.2 Output
Let the program help us do things internally, and output the results after doing the things.
print("你好呀")
print("欢迎使用xxx系统")
For example: Find all files ending in .png in a certain directory.
import os
for item in os.listdir("/Users/wupeiqi/PycharmProjects/gx_day01"):
if item.endswith('png'):
print(item)
-
Basic usage of output
print("郭德纲")
print("xqs")//Default newline, automatically add newline character at the end
-
Don't wrap
print("伤情最是晚凉天", end="") print("憔悴私人不堪言", end="")
print(“xqs”,end="") directly splice two sentences together
3.3 Data types
3.3.1 Integer type (int)
For example: 19, 180, 150.
19
20
300
300 + 19
2 * 6
80 - 77
100 / 10
98 % 10(求余数)
print(19)
print(300 + 19 )//也可以这样输出319
print(20)
3.3.2 String (str)
is used to represent information in this article in our lives, for example: "Beijing"
//Single quotes and double quotes are not sensitive, the following are all strings
# 单行文本
"111"
'222'
# 多行文本,可以写多行
"""111"""
'''123'''
'''xq
s'''
print("11")
print('22')
Strings can be added together, which is string concatenation. Can only be spliced between strings
"北京" + "yyds"
"北京yyds"
When a string and a number are multiplied, the result is the number of times the string is repeated.
"11" * 3
"111111"
"xqs" * 3
"xqsxqsxqs"//不是带三个引号
practise
print( 12 + 12 )
# 24,整型
print( "12" + "12" )
# "1212",字符串(文本)
Convert
str(19) # 19 -> "19" 转成字符串str(17)--->"17"
int("88") # "88" -> 88 转成整型int("20")---->20
int("联通") # 无法转换,报错
Practice questions
-
Calculate the quotient of the integer 50 multiplied by 10 divided by 5 and print the output.
print( 50 * 10 / 5 ) print( 50*10/5)
-
Calculate the remainder obtained by dividing the integer type 30 by 2 and print it out.
print( 30 % 2 )
-
Use string multiplication to create the string "I love my motherland" three times and concatenate them together to finally use print output.
print( 3 * "我爱我的祖国" )
-
See the code writing results (disable running code):
print( int("100")*3 )//300 print( int("123") + int("88") )//211 print( str(111) + str(222) )//"111222" print( str(111)*3 )//"111111111"
4.3.3 Boolean type (bool)
- True True
- False
1 > 2 -> False
1 == 2 -> False
"vefv" == "gwyu" -> False
22 == 22 -> True
Integer, string type -> Boolean value. //Generally str,int->bool
-
Integer type, 0 is converted to Boolean value as False, others are True//non-0 is True
print( bool(0) ) # False print( bool(-1) ) # True print( bool(100) ) # True
-
String, empty string is converted to Boolean value False, others are True // non-empty value is True
print( bool("") ) # False
print( bool("s") ) # True
print( bool(" ") ) # True空格为True
practise
print( int("8") > 7 ) //True
print( str(111) == 111 )//False
print( bool(-1) )//True
print( bool(0) )//False
print( bool("") )//False
print( bool("你好") )//True
print( True == True )//True
print( True == False )//False
print( bool("") == bool(0) )//True
3.4 Variables
Variables are the names/nicknames we give a value.
Format:变量名 = 值
addr = "中国北京"
print(addr)
print(addr)//"中国北京"
age = 18
name = "123"
is_success = 1 > 19 # False //is = 1>10--->False
addr = "中国" + "北京"
address = "中国" + "北京" + name # "中国北京123"
result = 1 == 2 # False
print(result)//False
3.4.1 Specifications
name = "储ou"
-
Variable names can only contain: letters, numbers, and underscores.
-
cannot start with a number
-
Cannot use Python built-in keywords
[‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]
practise
1 name = "吉诺比利"//T
2 name0 = "帕克"//T
3 name_1 = "邓肯"//T
4 _coach = "波波维奇"//T
5 _ = "卡哇伊"//T
6 1_year = "1990"//F
7 year_1_ = "1990"//T
8 _1_year = "1990"//T
9 nba-team = "马刺" //F
10 new*name = "伦纳德"//F
3.4.2 Memory pointing of variables
Memory pointing (how it is stored in the computer’s memory)
Scenario 1
name = "wupeiqi"
Create an area in the computer's memory to save the string "wupeiqi", and the name variable points to this area.
Scenario 2
name = "wupeiqi"
name = "Alex" //"wupeiqi"被抛弃
Create an area in the computer's memory to save the string "wupeiqi", and the name variable points to this area. Then a domain is created in the memory to save the string "alex". The name variable points to the area where "alex" is located, and no longer points to the area where "wupeiqi" is located (data that no one points to will be marked as garbage, as explained by automatic recycling)
Scenario 3
name = "wupeiqi"
new_name = name//new_name和name一起指向"wupeiqi"
Create an area in the computer's memory to save the string "wupeiqi", and the name variable points to this area. The new_name variable name points to the name variable. Because the variable name is pointed to, it will automatically point to the memory area represented by the name variable.
Scenario 4
name = "wupeiqi"
new_name = name
name = "alex"
Create an area in the computer's memory to store the string "wupeiqi", the name variable name points to this area (gray line), then new_name points to the memory area pointed to by name, and finally creates an area to store "alex", let The name variable points to the area where "alex" is located.
Scenario five
num = 18
age = str(num)//num为18,age为"18",两个指向不同地方
Create an area in the computer's memory to store the integer 18, and the name variable points to this area. Through type conversion, a string "18" is created in the memory according to the integer type 18, and the age variable points to the memory area where this string is stored.
practise
-
Look at the code writing results
alex_length = 3 wupeiqi_length = 18 total = alex_length + wupeiqi_length print(total) # 21
-
Look at the code writing results
nickname = "一米八大高个" username = nickname username = "弟弟" print(nickname) # "一米八大高个" print(username) # "弟弟"
nickname = "一米八大高个" username = nickname nickname = "弟弟" print(nickname) # "弟弟" print(username) # "一米八大高个"
nickname = "一米八大高个" username = nickname nickname = "弟弟" text = username + nickname print(text) # "一米八大高个弟弟"
-
Look at the code writing results
string_number = "20" num = int(string_number) data = string_number * 3 print(data) # "202020" value = num * 3 print(value) # 60
3.5 Notes
Code that is automatically ignored after being seen by the Python interpreter.
-
Single line comments
# 注释内容 快捷键: - win:control + ? - mac:commond + ?
-
Multi-line comments
""" 注释的内容 ... .. """
3.6 Input
Why do we need input?
! ! ! The input input defaults to a string format and is assigned to a variable.
text = input("提示信息")
print(text)//显示"提示信息",后输入数字按回车
name = input("请输入姓名:")
age = input("请输入年龄:")
email = input("请输入邮箱:")
text = name + age + email
print(text)
name = input("请输入姓名:")
age = input("请输入年龄:")
email = input("请输入邮箱:")
text = "我叫" + name + ",今年" + age + "岁,我的邮箱是" + email + "。"
print(text)
v1 = input("请输入数字:") # "100"
v2 = input("请输入数字:") # "200"
result = v1 + v2
print(result) # "100200"//字符串拼接
v1 = input("请输入数字:") # "100"
v2 = input("请输入数字:") # "200"
result = int(v1) + int(v2)
print(result) # 300//数字相加减
3.7 Conditional statements
If xx, then xx.
if 条件/真假 :
条件成立后会执行此处的代码//注意缩进
条件成立后会执行此处的代码
条件成立后会执行此处的代码
else:
不成立,走此处的代码
Case:
print("开始")
if True:
print("111")
print("222")
else:
print("666")
print("999")
print("结束")
print("开始")
if 1 > 2:
print("111")
print("222")
else:
print("666")
print("999")
print("结束")
print("开始")
num = 9
if num > 10: //if num < 10 :
print("111")
print("222")
else:
print("666")
print("999")
print("结束")
print("欢迎使用联通系统")
user = input("请输入用户名:")
pwd = input("请输入密码:")
if user == "wupeiqi" and pwd == "123":
print("恭喜你")
print("登录成功,获得奖励100w。")
else:
print("登录失败")
print("你是一个成为百万年薪的人")
print("程序结束")