Python Basics for Beginners: Syntax, Variables, Loops & Examples (Complete Guide for beginners)
π Python Basics
Syntax · Variables · Data Types · Operators · Loops · Functions · More
Python syntax defines the rules that code should be written. It is simple and easy to read, making it perfect for beginners. Python uses indentation (spaces) instead of curly braces or semicolons to define code blocks. Proper indentation is very important; otherwise it shows an error. Python also uses colons (:) and capital letters in specific places.
print("Hello, World!") # No semicolon needed # Indentation is important
Variables are used to store data values in a program. They are like containers that hold information such as numbers or text. In Python, you do not need to declare the type of a variable. Just write the variable name and assign it a value using =. You can use the variable name and assign it. Using (1) — variable names should be meaningful and follow correct naming rules.
name = "Ali" # starts with a letter age = 18 print(name, age)
Data types define the kind of data a variable can store. Choosing the correct data type helps to organize and manage data properly. Each data type has its own assigned value assigned to the variable on the same assigned basis.
x = 5x = 3.14x = "Hello"x = Truex = [1, 2, 3]x = {"name": "Ali"}x = 5 # int y = 3.14 # float z = "Hello" # str a = True # bool
print(type(x))
Python lets you perform operations on variables and values. Operators are used for calculations and decision-making. Python includes arithmetic, comparison, logical, and assignment operators. These are used in building logic and solving problems.
a = 10 b = 3 print(a + b) # 13 — Addition print(a - b) # 7 — Subtraction print(a * b) # 30 — Multiplication print(a / b) # 3.3 — Division print(a % b) # 1 — Modulus print(a ** b) # 1000 — Power
print(5 == 5) # True print(5 != 3) # True print(5 > 3) # True print(5 < 3) # False
print(True and False) # False print(True or False) # True print(not True) # False — conditional
Conditional statements are used to make decisions in a program. They execute different blocks of code based on conditions using if, elif, and else. In a conditional, the code inside the if block executes only if the condition is True. These are used in situations like checking age or login systems.
age = 18 if age >= 18: print("Adult") elif age >= 13: print("Teenager") else: print("Child")
Loops are used to repeat a block of code multiple times. Python mainly uses for and while loops. A for loop repeats for each item in a sequence. A while loop repeats as long as a condition is true. Loops are very useful for iterating over lists, strings, or repeating a set of operations.
# for loop for i in range(5): print(i) # Output: 0 1 2 3 4
# while loop i = 1 while i <= 3: print(i) i += 1 # Output: 1 2 3
Functions are reusable blocks of code that perform a specific task. Functions are defined using the def keyword. They can also take input values. Functions are defined once and can be called many times. They also can take input parameters and return values. Functions help organize code and avoid repetition.
def greet(name): print("Hello,", name) greet("Ali") # Hello, Ali greet("Sara") # Hello, Sara
def add(a, b): return a + b result = add(3, 5) print(result) # 8
Lists are used to store multiple items in a single variable. They are created using square brackets [ ]. Lists can store items of different types. You can access, add, remove, and update list items easily. Lists are one of the most useful data structures in Python.
fruits = ["apple", "banana", "mango"] print(fruits[0]) # apple fruits.append("grape") # Add item fruits.remove("banana") # Remove item print(len(fruits)) # 3 — length
Dictionaries are used to store data as key-value pairs. They are created using curly braces { }. Each value can be accessed using its unique key. Dictionaries are very useful for storing structured data like student records, user profiles, and settings.
student = { "name": "Ali", "age": 18, "grade": "A" } print(student["name"]) # Ali print(student["age"]) # 18
Comments are used to explain and describe code. They are not executed by Python. Single-line comments use #. Multi-line comments use triple quotes """. Comments help other programmers (and yourself) understand the code better, especially in larger projects.
# This is a single-line comment print("Hello") # inline comment """ This is a multi-line comment """ print("World")
Python basics are easy and beginner-friendly. By learning syntax, variables, data types, operators, conditions, loops, and functions, you now have a strong foundation in Python programming.
You can start writing real programs and keep practicing regularly. The more you code, the better you become!
π Best Python Programming Books for Beginners:
If you want to master Python coding from scratch to an advanced level, I highly recommend checking out these two excellent books:
-
π Book 1: Python Crash Course — A hands-on, project-based guide that is perfect for absolute beginners.
View on Amazon π -
π Book 2: Automate the Boring Stuff with Python — Great for learning practical coding to automate daily repetitive tasks.
View on Amazon π
Comments
Post a Comment