Introduction to Python: Start with 19 Syntax Basics!

Introduction to Python: Start with 19 Syntax Basics!

Follow the public account below to access organized technical columns on Python basics, web scraping, data analysis, office automation, etc.

Python is easy to learn but vast and profound.

Many claim to master Python, yet they cannot write Pythonic code and are unfamiliar with many commonly used packages.

The sea of learning is boundless; let’s start by understanding some fundamental concepts in Python.

Features of Python

Interpreted language, runs without compilation

Provides an interactive command line

Object-oriented programming paradigm

Cross-platform and good compatibility, runs on Windows, Mac, and Linux

Simple to use yet powerful

01 Chinese Encoding

Many students encounter garbled text when opening data, which is due to character set encoding issues. The default encoding for Linux and Mac is UTF8, while Windows uses ASCII. If the character set of the data encoding differs from the character set used in Python processing, garbled text will occur.

Additionally, I personally prefer to add the following content at the top of my Python code, where the second line declares the use of UTF8 character set.

#!/usr/bin/env python# coding:utf8

02 Variables

Variables in Python can be seen as containers that hold the values we need to use.

Python’s variable name requirements are similar to other languages: they can include letters, numbers, and underscores, but cannot start with a number, and are case-sensitive. Of course, I recommend using pure English for variable names and choosing meaningful names to help understand the role of each variable.

Python is a weakly typed language, so there is no need to declare the type of a variable when using it. Variables in Python include the following types: numbers, strings, lists, tuples, and dictionaries.

03 Numbers

Numbers include integers and floating-point numbers, corresponding to whole numbers and decimal numbers, with the latter having higher precision.

# Integer
a = 1
# Floating-point
b = 2.1
print a, b

04 Strings

A string is the text we often deal with, which can contain any length of content enclosed in single or double quotes. Note that Chinese characters and punctuation can only appear within strings; using a Chinese comma in the third line below will result in a Python error.

c = "Hello"
d = "你好"
print c, d

Using + can concatenate two strings.

print c + d

Using len() can get the length of a string.

print len("Hello World")

Slicing can access a specific character or segment within a string.

# Indexing starts from 0
c = "Hello World"  # Prints 'H', index 0 indicates the first character
print c[0]
# Prints 'd', negative index counts from the end
print c[-1]
# Using : returns a segment, with the colon indicating start and end indices
print c[1:5]  # Returns characters from index 1 to 4
print c[1:-1], c[:5], c[3:]

05 Lists

A list is like a queue that stores multiple variables in order. Lists are similar to strings, but each element in a string is a character, while each element in a list can be of any type.

# Define an empty list using [] and add an element to the end using append()
# To add to the front, use prepend()
a = []
a.append(1)
a.append(2.1)
a.append("Hello")
print a

Using len() can get the length of a list.

print len(a)

Accessing and assigning list elements by index is similar to strings.

print a[1], a[-1]
a[1] = 100
print a

Use del to delete a specific element from the list.

del a[0]
print a

06 Tuples

A tuple is similar to a list, with the only difference being that the elements in a tuple cannot be changed after initialization, so it can be understood as a read-only variable.

# Define a tuple using ()
a = (1, 2.1, "Hello")
# Attempting to modify an element in the tuple will result in an error
a[0] = 100

07 Dictionaries

A dictionary is an incredibly important variable type, using a key to access the corresponding value, which is a key-value pair data format.

# Define a dictionary using {}
a = {}
# Use key to assign value
a["k1"] = 1
a["k2"] = 2.1
a["k3"] = "Hello"

Thus, we can summarize the differences between dictionaries and lists. The elements in a list are ordered, accessed by index, while the elements in a dictionary are unordered, accessed by key.

# You can also assign values to dictionaries and lists simultaneously
li = [1, 2.1, "Hello"]
di = {"k1": 1, "k2": 2.1, "k3": "Hello"}

Use has_key() to check if a key exists in the dictionary.

print di.has_key("k4")

If accessing a nonexistent key, Python will raise an error. When assigning a value, if the key already exists, the new value will overwrite the existing value.

08 Comments

Commented code will not run; it can be seen as notes and explanations for oneself and other programmers, improving code readability.

# This is a single-line comment
# This is a multi-line comment

In Sublime, select the content to comment, and press Ctrl+/ to comment it.

09 Reserved Characters

In Python, some strings have specific functions, such as import, class, etc. When choosing variable names, avoid these reserved characters.

# The following variable assignment will raise an error
import = 1

10 Lines and Indentation

In Python, the boundaries of code blocks are not explicitly defined by symbols like braces but are determined by indentation. Code at the same indentation level belongs to the same block, and care must be taken with indentation when using for, while, if, try, etc.

11 Operators

Operators generate new variables based on existing ones, mainly including the following types:

Arithmetic operators: +, -, *, /, %, which correspond to addition, subtraction, multiplication, division, and modulus

Comparison operators: ==, !=, >, <, >=, <=, which correspond to equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to

Assignment operators: =, +=, -=, *=, /=, %=, which correspond to assignment, add assignment, subtract assignment, multiply assignment, divide assignment, modulus assignment

Logical operators: and, or, not, which correspond to logical conjunction, disjunction, and negation

a = 1
b = 2
print a + b
print a == b
# Equivalent to a = a + 3
a += 3
print a
c = True
d = False
print c and d

12 Conditions

When writing code, it is often necessary to make judgments based on certain conditions and execute different branches of code based on the results.

a = 1
# Single condition
if a == 1:
    print 11111
# Handle the branch when the condition is not met
if a == 2:
    print 22222
else:
    print 33333
# Multiple conditions, can add as many as needed
if a == 1:
    print 11111
elif a == 2:
    print 22222
else:
    print 33333

Note that whenever if and elif are present, the corresponding condition must be added, and attention should be paid to code indentation. In Sublime, typing if will bring up the corresponding prompt, making it easy to complete the code, and the cursor will automatically jump to the correct indentation when moving to the next line.

13 Loops

If you need to print the numbers from 1 to 100, you certainly wouldn’t write 100 lines of print code; instead, you would use a loop to handle such repetitive tasks.

14 While Loop

The while loop works on the principle that as long as a certain condition holds, the code inside the loop will keep executing until the condition no longer holds.

flag = 1
while flag < 10:
    print flag
    # Remember to modify the condition variable in the loop body
    # Otherwise, it may lead to an infinite loop
    flag += 1

15 For Loop

The for loop typically has a predetermined number of iterations, ending after iterating a flag variable from a starting value to an ending value.

# x starts from 0, and ends at 9
for x in xrange(0, 10):
    print x

The for loop can easily iterate through lists and dictionaries.

li = [1, 2.1, "Hello"]
dict = {"k1": 1, "k2": 2.1, "k3": "Hello"}
# Iterate through the list; here, item is just a temporary variable, you can use any name
for item in li:
    print item
# Iterate through all keys in the dictionary; here, key is also just a temporary variable, name is not important
for key in dict.keys():
    print key
# Iterate through all values in the dictionary; here, value is also just a temporary variable, name is not important
for value in dict.values():
    print value
# Simultaneously iterate through key and value
for key, value in dict.items():
    print key, value

16 Loop Control

Loop control mainly includes three types: pass, continue, break.

pass means doing nothing, just occupying a line of code; continue means immediately exit the current loop iteration and continue with the next iteration; break means immediately exit the loop, and subsequent iterations will not be executed.

for x in xrange(0, 10):
    if x == 5:
        pass
    else:
        print x
for x in xrange(0, 10):
    if x == 5:
        continue
    print x
for x in xrange(0, 10):
    if x == 5:
        break
    print x

17 Time

When processing data, many places will involve time, such as the time the data was generated. First, let’s introduce the concept of a timestamp, which refers to the number of seconds that have elapsed since January 1, 1970, 00:00:00, and can be an integer or a float, with the latter having higher precision.

Why is there a need for a concept like a timestamp? Because for the same moment, different people’s descriptions may vary, as text can take many forms, while a timestamp provides a unified representation of time, with each moment represented by a unique integer or float, which also facilitates calculations involving time differences.

# Let’s look at the current timestamp
import time
t = time.time()
print t, type(t)

Regarding timestamps, the most common operations involve converting between timestamps and time text, such as converting “2016-10-01 10:00:00” to a timestamp.

import time
# Convert time text to timestamp, accurate to seconds
a = "2016-10-01 10:00:00"
a = int(time.mktime(time.strptime(a, "%Y-%m-%d %H:%M:%S")))
print a
# Convert timestamp to time text
b = int(time.time())
b = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(b))
print b

Here, %Y, %m, etc., are time fields, where the former represents a four-digit year, and the latter represents a two-digit month.

File

File operations include writing content to a file and reading content from a file, using open() to open a file.

# Write to file
# Write mode will clear the file content when opening
fw = open("data.txt", "w")
# Append mode will keep the original content and continue writing
for x in xrange(0, 10):
    # Convert integer to text before writing
    fw.write(str(x))
    # You can also add a newline after each write,
    # 
 is the escape character indicating a newline
    # fw.write(str(x) + "\n")
fw.close()
# Read from file
fr = open("data.txt", "r")
# Read line by line; line is just a temporary variable, you can use any name
for line in fr:
    print line
    # If each line has a newline at the end, you can remove the newline character to make the content more compact
    # strip() can remove whitespace from both ends of the string
    # print line.strip()
fr.close()

18 Exceptions

Python code may encounter some predictable issues, such as accessing a nonexistent key in a dictionary.

If not handled, Python will raise an error and exit when a problem occurs, which may require starting over if it has run for a long time. Therefore, we need to catch and handle potential exceptions. The structure of an exception consists of try, except, else, and finally.

try:
    # Attempt to execute this code
    print 1 / 0
except Exception, e:
    # If an exception occurs, handle it
    # e is the type of exception that occurred
    print e
else:
    # If no error in try, execute subsequent work
    print "No error"
finally:
    # Code that will execute regardless of whether an error occurred
    print "This will always execute"

19 Functions

The purpose of functions is to modularize code, encapsulating reusable code into a function so that when needed, you can simply call the function without rewriting the code.

Using a function includes two parts: defining the function and calling the function. Additionally, functions can have one or more parameters, separated by commas, providing more flexibility for the function’s functionality.

# Define a function
def hello(name1, name2):
    print "Hello " + name1 + " and " + name2
# Call the function
hello("Python", "JavaScript")

Recommended Free Courses:
Web Frontend |Java Development | Python Data Analysis | Linux Cloud Computing | Software Testing | Big Data | IoT | Cybersecurity | C++
Unity Game | UI/UX Design | Film Editing | PMP
Introduction to Python: Start with 19 Syntax Basics!

Leave a Comment