SoFunction
Updated on 2025-03-01

Get started with Python basic knowledge quickly

This article uses code examples and you will see it at a glance. The content of getting started with Python basic knowledge is explained in detail from the aspects of basic syntax, variable types, operators and conditional statements. I hope this article will be helpful to beginners in Python.

1. Basic grammar

Basic syntax: including comments, Python identifiers, Python reserved words, etc.

Among them, comments are an important part of the program and can improve the readability and maintainability of the program. In Python, "#" is used to represent comments, and the content of comments will be ignored by the interpreter.

# I'm a commentprint("Hello, world!") # This is also annotation

Python identifiers refer to user-defined program entity names, such as variable names, function names, etc. The identifier must begin with a letter or an underscore and consist of letters, underscores, and numbers.

name = "Alice"
age = 18
_height = 170

Python reserved words refer to words that are given special meanings by the Python language, such as if, else, for, while, etc. Reserved words cannot be used as identifiers.

if name == "Alice":
    print("Hello, Alice!")
else:
    print("You're not Alice.")

2. Variable type

Python has five standard data types, namely Number, String, List, Tuple, and Dictionary.

Number types include integers (int), floats (floats), and complexes. You can view the type of a variable through the type() function.

age = 18
height = 170.5
complex_num = 2 + 3j
print(type(age)) # int
print(type(height)) # float
print(type(complex_num)) # complex

String type is one of the commonly used data types in Python, representing a series of characters. You can use single or double quotes to represent strings, or you can use three quotes to represent multiline strings.

str1 = 'Hello, world!'
str2 = "I'm Alice."
str3 = '''This
is
a 
multiline
string.'''
print(str1)
print(str2)
print(str3)

A list type is an ordered set of data that can contain different types of data. A list can be represented by brackets, and elements of the list can be accessed through subscripts.

list1 = [1, 2, 3, 4, 5]
list2 = ["apple", "banana", "orange", 123, 4.56]
print(list1[0]) # 1
print(list2[1]) # banana

Tuple types are similar to lists, but elements cannot be modified. Tuples can be denoted using brackets.

tuple1 = (1, 2, 3, 4, 5)
tuple2 = ("apple", "banana", "orange", 123, 4.56)
print(tuple1[0]) # 1
print(tuple2[1]) # banana

A dictionary type is a data type in the form of key-value pairs that can be used to store and find data. A dictionary can be represented by curly braces, and key-value pairs are separated by colons.

dict1 = {"name": "Alice", "age": 18, "gender": "female"}
dict2 = {1: "apple", 2: "banana", 3: "orange"}
print(dict1["name"]) # Alice
print(dict2[2]) # banana

3. Operators

Commonly used operators in Python include arithmetic operators, assignment operators, comparison operators, logical operators, etc.

Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), dividing (//), and modulus (%).

a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1

Assignment operators are used to assign values ​​to variables. Common assignment operators include =, +=, -=, *=, /=, etc.

a = 10
a += 2
a -= 3
a *= 2
a /= 5
print(a) # 3.2

Comparison operators are used to compare the sizes of two values. Common comparison operators include ==, !=, >, <, >=, <=, etc.

a = 10
b = 3
print(a == b) # False
print(a != b) # True
print(a > b) # True

Logical operators include and, or, and not, etc., which are used to make logical judgments on multiple conditions.

a = 10
b = 3
c = 5
print(a > b and b < c) # True
print(a < b or b > c) # False
print(not(a > b)) # False

4. Conditional statements

Commonly used conditional statements in Python, including if statements, if-else statements and if-elif-else statements.

The if statement is used to determine whether a condition is true, and if it is true, the specified statement block is executed.

age = 18
if age < 20:
    print("You're still young.")

The if-else statement is used to determine whether a condition is true. If it is true, the specified statement block is executed, otherwise the else statement block is executed.

age = 22
if age < 20:
    print("You're still young.")
else:
    print("You're not young anymore.")

The if-elif-else statement is used to judge multiple conditions, and if the first condition is not true, the second condition is judged, and so on. If all conditions are not true, the else statement block is executed.

age = 25
if age < 20:
    print("You're still young.")
elif age < 30:
    print("You're in your prime.")
else:
    print("You're not young anymore.")

5. Summary

This article elaborates on the content of getting started with Python basic knowledge from multiple aspects: basic syntax, variable types, operators and conditional statements. Hope this article will be helpful to beginners in Python.

This is the article about getting started with Python basic knowledge. For more information about learning Python basic knowledge, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!