Detailed Explanation of Core Python Syntax

Part One: Theoretical Foundation

1.1 What is a Programming Language?

Simple Understanding: A programming language is like a language we use to communicate with computers. Just as you chat with friends in Chinese or English, we use programming languages to tell computers what to do.

Features of Python:

  • Easy to Learn: The syntax is close to English, making it easy to understand.
  • Powerful: It can be used for web development, data analysis, artificial intelligence, etc.
  • Free and Open Source: Anyone can use it for free.

1.2 Basic Components of a Program

A Python program consists of the following basic parts:

  1. 1. Variables: Containers for storing data.
  2. 2. Data Types: Types of data (numbers, text, etc.).
  3. 3. Operators: Symbols for performing mathematical or logical operations.
  4. 4. Control Structures: Structures that control the flow of program execution (conditional statements, loops).
  5. 5. Functions: Reusable blocks of code.

Part Two: Variables and Data Types

2.1 Theory of Variables

What is a Variable? Imagine a variable as a labeled box:

  • Box: A place to store data.
  • Label: The name of the variable.
  • Content: The data stored in the variable.

Variable Naming Rules:

  • • Can only contain letters, numbers, and underscores.
  • • Cannot start with a number.
  • • Cannot use Python keywords (like if, for, etc.).
  • • Case-sensitive.

2.2 Theory of Data Types

Main data types in Python:

  1. 1. Numbers: Integers, floats.
  2. 2. Strings: Text content.
  3. 3. Booleans: True or False.
  4. 4. None: Represents nothing.

Part Three: Code Practice

3.1 First Python Program

# Print welcome message
print("Hello, World!")
print("Welcome to learning Python programming!")

Code Explanation:

  • <span>print()</span> is a function used to display content on the screen.
  • • The text inside the parentheses will be displayed as is.
  • <span>#</span> The content after this is a comment, which the computer will ignore; it is for human readers.

3.2 Variable Usage

# Create variables and assign values
name = "Xiao Ming"           # String type
age = 18                     # Integer type
height = 1.75                # Float type
is_student = True            # Boolean type (True or False)

# Use variables
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is Student:", is_student)

# Modify variable value
age = 19
print("Next year's age:", age)

Output:

Name: Xiao Ming
Age: 18
Height: 1.75
Is Student: True
Next year's age: 19

3.3 Data Type Operations

# Numeric operations
a = 10
b = 3

print("Addition:", a + b)      # 13
print("Subtraction:", a - b)      # 7
print("Multiplication:", a * b)      # 30
print("Division:", a / b)      # 3.333...
print("Integer Division:", a // b)  # 3
print("Remainder:", a % b)     # 1
print("Square:", a ** 2)     # 100

# String operations
first_name = "Zhang"
last_name = "San"
full_name = first_name + last_name  # String concatenation
print("Full Name:", full_name)

message = "Hello, " + full_name + "!"
print(message)

# Repeat string
laugh = "Ha" * 3
print(laugh)  # Hahaha

3.4 Data Type Conversion

# Conversion between different types
number_str = "123"      # This is the string "123", not the number 123
number_int = int(number_str)    # Convert to integer
number_float = float(number_str) # Convert to float

print("String:", number_str)
print("Integer:", number_int)
print("Float:", number_float)

# Convert number to string
age = 20
age_str = str(age)
print("I am " + age_str + " years old.")  # Must convert to string to concatenate

# Type checking
print(type(age))        # &lt;class 'int'&gt;
print(type(age_str))    # &lt;class 'str'&gt;

Part Four: Input and Output

4.1 Getting User Input

# Get user input
name = input("Please enter your name: ")
age = input("Please enter your age: ")

print("Hello, " + name + "! You are " + age + " years old.")

# Note: input() retrieves data as strings
# If a number is needed, type conversion is required
birth_year = input("Please enter your birth year: ")
current_year = 2024
age = current_year - int(birth_year)  # Convert to integer for calculation

print("You are approximately", age, "years old.")

Part Five: Practical Exercises

Exercise 1: Personal Information Collection

# Collect user information and display
print("=== Personal Information Collection ===")

name = input("Name: ")
age = input("Age: ")
city = input("City: ")
hobby = input("Hobby: ")

print("\n=== Your Information ===")
print("Name:", name)
print("Age:", age)
print("City:", city)
print("Hobby:", hobby)

Exercise 2: Simple Calculator

# Simple calculator
print("=== Simple Calculator ===")

# Get two numbers
num1 = float(input("Please enter the first number: "))
num2 = float(input("Please enter the second number: "))

# Perform calculations
print("\nCalculation Result:")
print("Addition Result:", num1 + num2)
print("Subtraction Result:", num1 - num2)
print("Multiplication Result:", num1 * num2)
print("Division Result:", num1 / num2)

Exercise 3: Temperature Conversion

# Celsius to Fahrenheit converter
print("=== Temperature Converter ===")

# Get Celsius temperature
celsius = float(input("Please enter the Celsius temperature: "))

# Conversion formula: Fahrenheit = Celsius × 9/5 + 32
fahrenheit = celsius * 9/5 + 32

print("Celsius Temperature:", celsius, "°C")
print("Fahrenheit Temperature:", fahrenheit, "°F")

Leave a Comment