Introduction to Python, Variables, and Input/Output Statements
Learning Objectives
Complementary materials available for free: Qianfeng Python Data Types
- Understand the fundamentals of the Python programming language
- Install and configure Python environment
- Experience interactive Python coding
- Apply PEP8 standards for spacing and line breaks
- Use comments to annotate code
- Define and utilize variables
- Know naming conventions and rules for identifiers
- Print specific content using Python
- Capture user input through code
Computer Architecture
A computer system consists of two main components: hardware and software. Writing code primarily involves implementing software functionalities.
What Is a Computer?
(Overview omitted)
Components of a Computer
A complete computer system includes both hardware and software systems.
What Are Programming Languages?
Computers operate using binary digits—0s and 1s—which form machine language instructions. These are the direct commands computers understand. However, humans write programs using high-level languages like English words and punctuation marks, which require translation through interpreters or compilers to be executed by machines.
Programming languages define the syntax and semantics for creating computer programs. They allow developers to express logic and algorithms in human-readable formats before converting them into executable machine code.
Introduction to Python
Python is an interpreted programming language and one of the most widely used languages today.
Applications of Python
- Web application development
- Automation scripts for system administration and server management
- Web scraping tools
- Scientific computing
- Desktop applications
- Server-side software
- Game development
Installing and Setting Up Python
Managing Python Packages with pip
pip install <package_name>installs a specified packagepip uninstall <package_name>removes a specified packagepip freezelists all installed packages in a format suitable for dependency filespip install -r requirements.txtinstalls packages listed in a file
Changing pip Download Sources
By default, pip downloads packages from http://files.pythonhosted.org/, which can be slow due to network issues. To speed up installation, you can switch to a domestic mirror:
pip install <package_name> -i <mirror_url>
Common domestic mirrors:
- Aliyun: http://mirrors.aliyun.com/pypi/simple
- University of Science and Technology of China: https://pypi.mirrors.ustc.edu.cn/simple/
- Douban: http://pypi.douban.com/simple
- Tsinghua University: https://pypi.tuna.tsinghua.edu.cn/simple/
- University of Science and Technology of China (alternative): http://pypi.mirrors.ustc.edu.cn/simple/
Development Tools
- PyCharm Community Edition
Interactive Programming
Execute Python code directly within the terminal.
Comments
# Single-line comment
'''
Multi-line comment
'''
"""
Alternative multi-line comment
"""
Variables and Data Types
In Python, data values have distinct types.
Checking Data Types
Use the built-in function type() to determine the type of a variable.
Note: In Python, variables themselves do not carry type information; instead, it's the value they reference that determines the type. This is a characteristic of dynamically typed languages.
Identifiers and Keywords
An identifier refers to names assigned to variables, modules, functions, or classes.
Rules for Naming Identifiers
- Must consist of letters, numbers, and underscores; cannot start with a number
- Case-sensitive (there are 52 English letters)
- Cannot use reserved keywords (e.g.,
if,for) which have special meanings in Python
Best Practices
- Choose meaningful names
- Follow established naming conventions:
- CamelCase: First letter lowercase, subsequent words capitalized (e.g.,
userNameAndPassword) - PascalCase: All words capitalized (e.g.,
PersonModel) - Snake_case: Words separated by underscores (e.g.,
user_name_and_password)
- CamelCase: First letter lowercase, subsequent words capitalized (e.g.,
In Python:
- Variables, functions, and modules follow snake_case
- Classes use PascalCase
Output Statements
Python uses the built-in function print() to display output.
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Where:
value: Content to print (can be single or multiple values)sep: Separator between multiple valuesend: Ending character after printing (default is newline)file: Output stream destination
Input Statements
The built-in function input() captures user input.
password = input("Enter password: ")
Note: All inputs are returned as strings (str).