[Python Basics] From Hello, world to functions, you can read it in a few minutes~

Hello everyone, I am Brother Latiao~
Python is a high-level programming language that is easy to learn and use. Here is a small portion of the basics for getting started with Python:

Install Python:

First you need to download and install Python from the official website. The installation process is very simple, just follow the steps of the installation wizard.

If you really can’t, you can find Latiaoge’s business card at the bottom of the article, just go to Latiaoge to get the installation package and installation tutorial!

Click to jump to Latiaoge business cardinsert image description here

Write your first Python program

After the installation is complete, you can use any text editor to write Python programs. For example, on Windows, you can use Notepad or Notepad++ to open a text editor, then copy and paste the following code:

print("Hello, world!")

Save this file and name it "hello.py". At a command prompt or terminal, change to the directory where the file is located and enter the following command:

python hello.py

This command will run the Python program and output "Hello, world!".

Variables and Data Types

In Python, variables are containers used to store values. Variables can store different types of values ​​such as integers, floats, strings, etc. Here are examples of storing variables and using different data types:

x = 5
y = 3.14
name = "John"
is_student = True

In the above example, the variable "x" stores an integer, the variable "y" stores a float, the variable "name" stores a string, and the variable "is_student" stores a Boolean value.

operator

Python supports various operators such as arithmetic operators, comparison operators, logical operators, etc. Here are some examples:

Arithmetic operators:

x = 5
y = 3
print(x + y)  # 输出8
print(x - y)  # 输出2
print(x * y)  # 输出15
print(x / y)  # 输出1.6666666666666667
print(x % y)  # 输出2
print(x ** y) # 输出125

Comparison operators:

x = 5
y = 3
print(x == y) # 输出False
print(x != y) # 输出True
print(x > y)  # 输出True
print(x < y)  # 输出False
print(x >= y) # 输出True
print(x <= y) # 输出False

Logical Operators:

x = 5
y = 3
print(x > 0 and y < 10) # 输出True
print(x > 0 or y > 10)  # 输出True
print(not x > y)        # 输出False

control flow statement

Python supports various control flow statements, such as conditional statements, loop statements, etc. Here are some examples:

Conditional statements:

x = 5
if x > 0:
    print("x is positive")
elif x == 0:
    print("x is zero")
else:
    print("x is negative")

loop statement:

# while循环
i = 0
while i < 5:
    print(i)
    i += 1

# for循环
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

function

Python supports functions, which are reusable blocks of code that can receive parameters and return values. Here is an example of a simple function:

def square(x):
    return x * x
    
print(square(5)) # 输出25

In the example above, the function "square" takes an argument "x" and returns the square of that argument.

These are detailed tutorials on the basics of getting started with Python. After mastering the basic concepts, you can move on to learn advanced concepts and applications of Python.


insert image description here

Guess you like

Origin blog.csdn.net/AI19970205/article/details/130326174
Recommended