Python Basics: Data Types, Operators, Control Flow, and Common Functions
This comprehensive Python cheat‑sheet covers fundamental data types, arithmetic and comparison operators, boolean logic, special characters, file I/O, OS utilities, string manipulation, list and dictionary operations, control‑flow statements, loops, exception handling, and basic function, module, and package usage, providing concise examples for each concept.
This guide presents a concise reference of essential Python fundamentals for beginners and intermediate learners.
Primary Data Types : boolean, integer, float, string, list, dictionary, and their literal representations.
Arithmetic Operators : + (addition), - (subtraction), * (multiplication), / (division), ** (exponent), // (floor division), % (modulo).
Comparison Operators : == , != , > , < , >= , <= .
Boolean Logic : and , or , not .
Special Characters : # (comment), \n (newline), \ (escape).
File Read/Write : f = open('path') – open a file; f.read() – read entire content; f.readline() – read one line; f.write('text') – write text; f.close() – close the file.
OS Module : import os – import standard library; os.getcwd() – current directory; os.makedirs('dir') – create directories recursively; os.chdir('dir') – change working directory; os.listdir() – list directory contents.
String Operations : Access characters with String[i] or slices String[i:j] ; Methods include upper() , lower() , count(x) , find(x) , replace(x, y) , strip() , join() , format(x) .
List Operations : Create with list = [] ; Indexing, slicing, list.append(x) , list.extend(L) , list.insert(i, x) , list.remove(x) , list.pop(i) , list.clear() , list.sort() , list.reverse() , list.copy() . Delete element with del list[i] .
Dictionary Operations : Create with dict = {} ; Access/assign via dict[key] = value ; Delete with del dict[key] ; Methods: keys() , values() , items() , pop() , update(D) , clear() , copy() .
Control Flow : Conditional statements using if , elif , else ; Loop constructs: while condition: , for i in range() , for k, v in dict.items(): ; Loop control with break and continue . Exception handling with try: , except: , else: .
Functions, Modules, Packages : Define functions with def func(): and return values using return ; Import modules via import module , import module as alias , from module import name , from module import name as alias ; Import from packages with import package.module or from package import module .
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.