Quick Start with Python: A Must-Read for Self-Learners from Zero to Proficiency!

1. What is Python?

Quick Start with Python: A Must-Read for Self-Learners from Zero to Proficiency!

Python is a high-level programming language developed by Dutch programmer Guido van Rossum in the late 1980s. Its greatest feature is its concise code and intuitive syntax, making programming feel more like writing in natural language, allowing even complete beginners to understand easily.

Imagine you want the computer to output “Hello, World!” In Python, you only need a simple line of code:

print("Hello, World!")

In this line of code, print() is a built-in function in Python specifically used for outputting content, and the content inside the parentheses is the information you want to output. Isn’t it super simple?

2. Installing and Running Python

To write and run Python code, you first need to install the Python interpreter. Go to the official Python website (https://www.python.org/), download the installation package suitable for your operating system (Windows, Mac, or Linux), and follow the installation wizard to complete the installation.

Once installed, you can run Python code in two ways:

  1. Command Line Interactive Mode: On Windows, press Win+R keys, type cmd to open the command prompt; on Mac or Linux, open the terminal. Type python or python3 to enter the Python interactive environment. In this environment, you can type a line of code and press enter to see the result immediately, which is great for quickly testing code.
  1. Text Editor + Terminal: Use text editors like Visual Studio Code or Sublime Text to write Python code, save it as a .py file (for example, test.py). Then switch to the directory where the file is located in the terminal and type python test.py or python3 test.py to run the code.

Quick Start with Python: A Must-Read for Self-Learners from Zero to Proficiency!

3. Basic Syntax of Python

1. Variables and Data Types

In Python, a variable is like a box used to hold data. You can assign values to variables and use them to store and manipulate data. Python supports various data types, commonly including:

  • Integers (int): such as 1, -5, 100, etc.;
  • Floating-point numbers (float): numbers with decimal points, such as 3.14, -2.5;
  • Strings (str): text enclosed in single quotes or double quotes : for example, ‘Hello’, “Python is great”
  • Boolean values (bool): only two values, True and False, commonly used for logical judgments.

Example code:

# Define integer variableage = 25# Define float variableheight = 1.75# Define string variablename = "Alice"# Define boolean variableis_student = Trueprint(age)print(height)print(name)print(is_student)

2. Operators

Operators in Python are used to perform operations on data, commonly including:

  • Arithmetic operators: +(addition), (subtraction), *(multiplication), /(division), //(floor division), %(modulus), **(exponentiation);
  • Comparison operators: ==(equal), !=(not equal), >(greater than), <(less than), >=(greater than or equal to), <=(less than or equal to);
  • Logical operators: and(and), or(or), not(not).

Example code:

# Arithmetic operationsnum1 = 10num2 = 3print(num1 + num2)  # Output 13print(num1 / num2)  # Output 3.3333333333333335print(num1 // num2)  # Output 3print(num1 % num2)  # Output 1print(num1 ** num2)  # Output 1000# Comparison operationsprint(num1 > num2)  # Output Trueprint(num1 == num2)  # Output False# Logical operationsa = Trueb = Falseprint(a and b)  # Output Falseprint(a or b)  # Output Trueprint(not a)  # Output False

3. Control Flow Statements

Control flow statements allow the program to execute different blocks of code based on different conditions. Common control flow statements include:

  • Conditional statements (if-elif-else): used to execute different code based on conditions.
score = 85if score >= 90:    print("Excellent")elif score >= 80:    print("Good")else:    print("Average")

Loop statements (for and while): used to repeatedly execute a block of code.

# For loop example: iterate through a listfruits = ["apple", "banana", "cherry"]for fruit in fruits:    print(fruit)# While loop example: calculate the sum from 1 to 10sum_num = 0i = 1while i <= 10:    sum_num += i    i += 1print(sum_num)

4. Python Functions

A function is a reusable block of code designed to perform a specific task. In Python, you can define a function using the def keyword.

Example code:

# Define a function to calculate the sum of two numbersdef add(num1, num2):    return num1 + num2result = add(5, 3)print(result)  # Output 8

5. Quick Hands-On Project: Create a Simple Number Guessing Game

Now, let’s combine the knowledge we’ve learned to create a simple number guessing game. The rules of the game are: the program randomly generates a number between 1 and 100, the player inputs their guess, and the program indicates whether the guess is too high or too low until the player guesses correctly.

import random# Generate random numbersecret_number = random.randint(1, 100)guess = 0attempts = 0print("Welcome to the Number Guessing Game!")print("I have thought of a number between 1 and 100, come and guess it!")while guess != secret_number:    try:        guess = int(input("Please enter your guess: "))        attempts += 1        if guess < secret_number:            print("Too low, try again!")        elif guess > secret_number:            print("Too high, think again!")    except ValueError:        print("Please enter a valid integer!")print(f"Congratulations! You guessed it right! You guessed {attempts} times.")

Quick Start with Python: A Must-Read for Self-Learners from Zero to Proficiency!

Leave a Comment