Fundamentals 9 min read

Master Python Basics: Variables, Data Types, Operators, and Control Flow

This guide introduces Python's core fundamentals, covering its concise syntax, commenting styles, dynamic variables, primary data types, input/output functions, a full range of operators, control‑flow statements, and essential data structures such as lists, tuples, dictionaries and sets, complete with examples and visual illustrations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Master Python Basics: Variables, Data Types, Operators, and Control Flow

Introduction

Python is a simple, powerful high‑level programming language with concise syntax, strong readability, and excellent cross‑platform support. It is widely used in data analysis, artificial intelligence, web development, automation scripts, and web crawling.

Basic Syntax

Comments

Single‑line comments start with # . Multi‑line comments can be enclosed in triple single quotes ''' comment ''' or triple double quotes """ comment """ .

Variables

Python is dynamically typed; variables are created by assignment without explicit declaration, and their type can change at runtime.

Basic Data Types

Numbers (integers in decimal, octal, hexadecimal, floating‑point, complex), strings, booleans, and type conversion are supported.

Input and Output

Use input() to read user input and print() to display output.

Operators and Expressions

Python provides arithmetic, assignment, comparison, logical, and bitwise operators. Operator precedence follows standard rules, and parentheses can override the default order.

Assignment Operators

Standard assignment = and compound forms such as += , -= , *= , etc.

Comparison Operators

== , != , > , < , >= , <= .

Logical Operators

and , or , not .

Bitwise Operators

AND & , OR | , XOR ^ , NOT ~ , left shift << , right shift >> .

Conditional Expression

Inline ternary: r = a if a > b else b is equivalent to the multi‑line if / else block.

Control Flow Statements

Sequential Structure

if condition: # single branch

if condition: # double branch # branch1 else: # branch2

if condition1: # branch1 elif condition2: # branch2 else: # branchN

Loop Structure

while condition: # loop body else: # executed when loop ends normally

for var in iterable: # loop body else: # executed when loop ends normally

Loop Control

break exits the innermost loop, continue skips to the next iteration, and pass is a null statement.

Sequences

Lists

Creation: my_list = [] or my_list = list() .

Adding elements: my_list.append(item) (single item), my_list.extend(iterable) (multiple items), concatenation my_list + other_list , my_list.insert(index, item) .

Modification: my_list[index] = new_value .

Deletion: del my_list[index] , my_list.pop() (last element), my_list.remove(value) (first occurrence).

Access: my_list[index] , my_list.index(value, start, end) , my_list.count(value) , membership value in my_list .

Slicing: my_list[start:end:step] (step defaults to 1; end is exclusive).

Copying: shallow copy via assignment ( b = a ) or b = a.copy() ; deep copy via copy.deepcopy(a) .

Sorting: my_list.sort() (in‑place), sorted(my_list) (returns new list), reverse with list.reverse() or sorted(my_list, reverse=True) .

Random shuffle: random.shuffle(my_list) .

Built‑in functions: all(my_list) , any(my_list) , max(my_list) , min(my_list) , sum(my_list) , zip(list_a, list_b) , enumerate(my_list) .

List comprehension: [expression for var in range(...)] .

Basic data types diagram
Basic data types diagram

Tuples

Creation: my_tuple = (value1, value2, ...) or my_tuple = tuple(iterable) .

Unpacking allows simultaneous assignment to multiple variables.

Dictionaries

Creation: my_dict = {} or my_dict = dict() . dict.fromkeys(keys, value) creates a dict with given keys.

Access: my_dict[key] , my_dict.get(key, default) , my_dict.items() , my_dict.keys() , my_dict.values() .

Adding/Modifying: my_dict[key] = value (adds if key absent, updates if present).

Sets

Creation: my_set = set() or my_set = {item1, item2, ...} .

Operations: my_set.add(item) , my_set.remove(item) , my_set.pop() , my_set.clear() .

Set algebra: union a | b , intersection a & b , difference a - b , symmetric difference a ^ b , subset test a < b .

Operator precedence diagram
Operator precedence diagram
PythonData StructuresvariablesProgramming Basicscontrol flow
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.