Home Backend Development Python Tutorial python must memorize entry code

python must memorize entry code

Oct 25, 2023 am 09:31 AM
python

Python is a simple and easy-to-learn programming language, suitable for beginners to get started. The following are some necessary Python entry codes to help you get started programming quickly:

1. Output Hello World

print("Hello World!")
Copy after login

2. Variables and data types

# 定义变量并赋值
name = "Alice"
age = 18
height = 1.65
is_student = True
# 打印变量
print(name)
print(age)
print(height)
print(is_student)
# 数据类型转换
age_str = str(age)
height_int = int(height)
is_student_str = str(is_student)
Copy after login

3. Input

# 输入字符串
name = input("请输入您的姓名:")
print("您的姓名是:", name)
# 输入数字
age = int(input("请输入您的年龄:"))
print("您的年龄是:", age)
Copy after login

4. Conditional judgment

age = int(input("请输入您的年龄:"))
if age >= 18:
print("您已经成年了!")
else:
print("您还未成年!")
Copy after login

5. Loop

# while循环
count = 0
while count < 5:
print("当前计数:", count)
count += 1
# for循环
for i in range(5):
print("当前计数:", i)
Copy after login

6. Lists and dictionaries

# 列表
fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]
print(fruits[0]) # 输出apple
fruits.append(&#39;grape&#39;) # 添加元素
print(fruits) # 输出[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;grape&#39;]
# 字典
person = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 18, &#39;gender&#39;: &#39;female&#39;}
print(person[&#39;name&#39;]) # 输出Alice
person[&#39;height&#39;] = 1.65 # 添加键值对
print(person) # 输出{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 18, &#39;gender&#39;: &#39;female&#39;, &#39;height&#39;: 
1.65}
Copy after login

7. Functions

# 定义函数
def greet(name):
print("Hello,", name)
# 调用函数
greet("Alice")
Copy after login

8.Exception handling

try:
num = int(input("请输入一个数字:"))
result = 10 / num
print("结果:", result)
except ZeroDivisionError:
print("除数不能为0!")
except ValueError:
print("请输入一个有效的数字!")
Copy after login

The above are some examples of Python entry code, I hope it can help you start learning Python programming. If you have any questions, please feel free to ask.

The above is the detailed content of python must memorize entry code. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...

See all articles