Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Introduction to Python, Variables, and Input/Output Statements

Tech May 7 4

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 package
  • pip uninstall <package_name> removes a specified package
  • pip freeze lists all installed packages in a format suitable for dependency files
  • pip install -r requirements.txt installs 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:

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

  1. Must consist of letters, numbers, and underscores; cannot start with a number
  2. Case-sensitive (there are 52 English letters)
  3. Cannot use reserved keywords (e.g., if, for) which have special meanings in Python

Best Practices

  1. Choose meaningful names
  2. 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)

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 values
  • end: 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).

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.