Quick Introduction to Python Programming: 19 Essential Syntax Elements
This article provides a concise, step‑by‑step guide to the most important Python syntax—including encoding, variables, data types, operators, control flow, file handling, and functions—offering beginners a solid foundation for rapid entry into Python development.
Many people hear that Python is easy to learn, has good prospects, and high salaries, so they want a fast way to get started; this guide presents the first step: 19 essential Python syntax topics.
Python Features : interpreted language, interactive shell, object‑oriented, cross‑platform, simple yet powerful.
01 Encoding : Use UTF‑8 to avoid garbled characters; add
#!/usr/bin/env python
# coding:utf8at the top of scripts.
02 Variables : Containers for values; names may contain letters, digits, underscores (not starting with a digit) and are case‑sensitive; Python is weakly typed. Types include numbers, strings, lists, tuples, dictionaries.
03 Numbers : Integers and floats. Example:
# integer
a = 1
# float
b = 2.1
print a, b04 Strings : Enclosed in single or double quotes; can concatenate with +, get length with len(), slice with [start:stop]. Example:
c = "Hello"
d = "你好"
print c + d
print len(c)
print c[0]
print c[1:5]05 Lists : Ordered collections, defined with [], mutable. Example:
a = []
a.append(1)
a.append(2.1)
a.append("Hello")
print a
print len(a)
print a[1], a[-1]
a[1] = 100
print a
del a[0]
print a06 Tuples : Immutable ordered collections, defined with (). Example:
a = (1, 2.1, "Hello")
# a[0] = 100 # raises error07 Dictionaries : Key‑value mappings, defined with {}. Example:
a = {}
a["k1"] = 1
a["k2"] = 2.1
a["k3"] = "Hello"
print a.has_key("k4") # returns False08 Comments : Use # for single‑line comments; multi‑line comments can be created by repeating #.
09 Reserved Words : Avoid using Python keywords (e.g., import, class) as identifiers.
10 Indentation : Code blocks are defined by consistent indentation rather than braces.
11 Operators : Arithmetic (+, -, *, /, %), comparison (==, !=, >, <, >=, <=), assignment (=, +=, etc.), logical (and, or, not). Example:
a = 1
b = 2
print a + b
print a == b
c = True
d = False
print c and d12 Conditionals : Use if, elif, else with proper indentation. Example:
a = 1
if a == 1:
print "11111"
elif a == 2:
print "22222"
else:
print "33333"13 Loops : Perform repetitive tasks.
14 while Loop : Executes while a condition is true; remember to modify the loop variable. Example:
flag = 1
while flag < 10:
print flag
flag += 115 for Loop : Iterate over a range, list, or dictionary. Example:
for x in xrange(0, 10):
print x
li = [1, 2.1, "Hello"]
for item in li:
print item
for key in dict.keys():
print key
for key, value in dict.items():
print key, value16 Loop Control : pass (do nothing), continue (skip to next iteration), break (exit loop). Example:
for x in xrange(0, 10):
if x == 5:
pass
else:
print x17 Time : Unix timestamp represents seconds since 1970‑01‑01. Convert between timestamp and readable time using time.time(), time.mktime(), time.strftime(), time.strptime(). Example:
import time
t = time.time()
print t, type(t)18 File I/O : Open files with open() in write ( w) or append ( a) mode, write strings, read lines, and close files. Example:
fw = open('data.txt', 'w')
for x in xrange(0, 10):
fw.write(str(x))
fw.close()
fr = open('data.txt', 'r')
for line in fr:
print line.strip()
fr.close()19 Exceptions : Handle predictable errors with try, except, else, finally. Example:
try:
print 1 / 0
except Exception as e:
print e
else:
print "No error"
finally:
print "Always runs"20 Functions : Define reusable code blocks with def, call them with arguments. Example:
def hello(name1, name2):
print "Hello " + name1 + " and " + name2
hello("Python", "JavaScript")Thank you for reading; this concludes the first step—19 essential Python syntax elements—for quickly getting started with Python programming.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
