Mastering All Basic Knowledge of Python

关注👆公众号、回复「 python」领零基础教程!来源于网络,侵删

Python is a widely used high-level programming language, emphasizing code readability and a concise syntax. This article will introduce all the basic knowledge of Python through one article. The basic knowledge of Python can be divided into the following aspects:

[Tutorial The way to obtain it is at the end of the article!!]

[Tutorial The way to obtain it is at the end of the article!!]

Part 1: Data Types

Part 2: Control Flow

Part 3: File Handling

Part 4: Modules and Packages

Part 5: Exception Handling

Part 6: Functional Programming

Part 7: Object-Oriented Programming

Part 8: Iterators and Generators

Part 1: Data Types

# Integers, Floats, and Stringsx = 520y = 3.14name = “JoJo”# Lists, Tuples, Dictionaries, and Setsmy_list = [1, 2, 3]my_tuple = (4, 5, 6)my_dict = {‘name’: ‘JoJo’, ‘age’: 7}my_set = {8, 9, 10}

Part 2: Control Flow

# Conditional Statementsif m > 0: print(“m is positive”)elif m < 0: print(“m is negative”)else: print(“m is zero”)# For Loopfor n in range(10): print(n)# While Loopwhile p > 0: print(p) p -= 1

Part 3: File Handling

# Opening a Filefile = open(‘file.txt’, ‘r’)

# Reading a File

content = file.read()print(content)# Writing to a Filefile = open(‘file.txt’, ‘w’)file.write(“Hello, JoJo !”)

# Closing a File

file.close()

Part 4: Modules and Packages

# Importing Modulesimport math# Using Functions from Modulessqrt_value = math.sqrt(25)print(sqrt_value)# Creating Custom Modules and Packages# mymodule.pydef greet(): print(“Hello, create mymodule!”)# Using Custom Modules and Packagesimport mymodulemymodule.greet()

Part 5: Exception Handling

# Using try-except for Exception Handling

try: result = 10 / 0

dill_with()

pass

except X-Error: print(“Error: Division by zero”)

Part 6: Functional Programming

# Defining Functionsdef sum(a, b): return a + b

def dill_with(s):

print(length(s))# Calling Functionsresult = sum(3, 4)print(result)

s = “Hello !”

dill_with(s)

Part 7: Object-Oriented Programming

# Defining Classesclass Person: def __init__(self, name): self.name = name def greet(self): print(f“Hello, {self.name}!”)

# Creating Objectsperson = Person(“JoJo !”)person.greet()

Part 8: Iterators and Generators

# Iteratorsmy_list = [6, 66, 666]iter_char = iter(my_list)print(next(iter_char))# Generatorsdef square_generator(i): for j in range(i): yield j**2for p in square_generator(5): print(p)

How to Obtain:

  1. Like + View Again

  2. Reply in the public account: “python”

Get the latest 2024 Python beginner learning materials,Reply in the background:Python

Leave a Comment