Python Basics: From Zero to Core Concepts with Practical Code Examples
This tutorial walks through essential Python fundamentals—including comments, naming conventions, variable assignment, input/output functions, common data types, type conversion, and arithmetic, assignment, relational, and membership operators—providing clear explanations and concrete code snippets for each concept.
Comments in Python
Comments explain what the code does and are ignored by the interpreter. Python supports two forms:
Single‑line comments start with #.
Multi‑line comments are enclosed by triple quotes ''' ... ''' or """ ... """.
# This is a single‑line comment
'''This is a
multi‑line comment'''Identifiers and Naming Conventions
Identifiers name variables, functions, classes, modules, etc. Rules:
Only letters, digits, and underscores are allowed.
Must start with a letter or underscore, not a digit.
Case‑sensitive.
Cannot be a Python keyword.
Common Python keywords include False, None, True, and, as, assert, async, await, break, class, continue, def, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield.
Variable Declaration and Assignment
Variables store values; Python does not require explicit type declaration. Assignment uses the = operator.
name = "Xiaoxi"
print(name)
age = 16
print(age)
num = 4.31
print(num)
# Multiple assignment
x = y = z = 10
print(x, y, z)
a, b = 23, 4.31
print(a, b)Printing Output
The print() function displays values. Important parameters: sep: separator between items (default space). end: string appended after the last value (default newline). file: a file‑like object to write to (default sys.stdout).
num = 123
num1 = 4.12
name = "Wu Yi"
print(num)
print(num1, name)
print(num1, name, "badminton", sep="****")
print(num1, name, "badminton", end="---")Input Function
input(prompt)reads a line from standard input and returns it as a string.
password = input("Please enter your password: ")
print(password)
age = input("How old are you? ")
print(age)Common Data Types
Python 3 defines six built‑in types:
Number ( int, float, bool)
String ( str)
List ( list)
Tuple ( tuple)
Dictionary ( dict)
Set ( set)
# Number examples
num = 32
print(num)
# Float example
num1 = 3.21
print(num1)
# Bool examples
bool1 = True
bool2 = False
print(bool1, bool2)
# List example
list1 = ['Zhang San', 'Li Si']
print(list1)
# Tuple example
tup = (12, 43, True, "world")
print(tup)
# Dict example
user = {"name": "Li Si", "age": 35, "sex": "M", "weight": 183}
print(user)
# Set example
set1 = {121, 67.36, True, False}
print(set1)Type Conversion Functions
Convert between types using built‑in functions: int(x, base=10) – to integer. float(x) – to floating‑point. str(x) – to string. bool(x) – to boolean (zero, empty, or None become False). list(x) – to list (iterable only). tuple(x) – to tuple.
# int → str
num = 22
s = str(num)
print(s, type(s))
# str → int (valid string)
age_str = "18"
age = int(age_str)
print(age, type(age))
# float conversion error example
float_str = "hello123"
# float(float_str) # raises ValueError
# bool examples
print(bool(100)) # True
print(bool(0)) # False
print(bool("")) # False
# list from tuple
t = (1, 2, 3)
print(list(t))
# tuple from string
s = "123"
print(tuple(s)) # ('1','2','3')
# error: list from int
# list(12) # raises TypeErrorArithmetic Operators
Supported operators:
+ # addition
- # subtraction
* # multiplication
/ # division
% # modulo
** # exponentiation
// # floor divisionExample:
a = 16
b = 8
c = 5
print(a + b) # 24
print(a - b) # 8
print(a * b) # 128
print(a / b) # 2.0
print(a % c) # 1
print(a // 7) # 3Arithmetic with Strings
Using + on strings concatenates them; mixing numbers and strings raises TypeError.
str1 = "hello"
str2 = "world"
print(str1 + str2) # hello world
# print(str1 + 5) # TypeErrorAssignment Operators
Simple assignment uses =. Compound operators combine an operation with assignment:
c = a + b # same as c += b
c += 5 # c = c + 5
c -= 2 # c = c - 2
c *= 3 # c = c * 3
c /= 4 # c = c / 4
c %= 5 # c = c % 5
c **= 2 # c = c ** 2
c //= 2 # c = c // 2Relational Operators
Compare values and return a Boolean:
a = 12
b = 31
c = 12
print(a < b) # True
print(a > b) # False
print(a >= b) # False
print(a <= b) # True
print(a == c) # True
print(a != b) # True
# String comparison follows ASCII order
print("a" > "f") # FalseMembership Operators
Check presence in sequences:
lst = [10, 34, 76, 89, 32, True]
print(34 in lst) # True
print(8.87 in lst) # False
print(93 not in lst) # True
s = "hello"
print('h' in s) # True
print('w' not in s) # TrueThe article concludes with a reminder that consistent practice is essential for mastering Python fundamentals.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
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.
