0% found this document useful (0 votes)
6 views2 pages

Python Basics Full Guide

Uploaded by

zebakausar656
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Basics Full Guide

Uploaded by

zebakausar656
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

■ Python Basics: Syntax, Variables, Data Types &

Operators (Full Easy Guide)

This guide will explain the basic building blocks of Python programming in a simple way with
examples. We will cover syntax, variables, data types, and operators.

1. Syntax
Syntax is like grammar in English. If you don't follow it, the computer will not understand you.
In Python, syntax rules include:
1 Use indentation (spaces) to define blocks of code.
2 Keywords like if, else, while, for are reserved words.
3 Use # for comments.
4 Strings can be written in single or double quotes.
Example:
print("Hello, World!") # This prints text
if 5 > 2:
print("Five is greater than two!")

2. Variables
Variables are like boxes where we store information. Each box has a name. You can put numbers,
text, or other things in it.
Example:
name = "Yousuf" # text
age = 20 # number
height = 5.9 # decimal
is_student = True # boolean
print(name, age, height, is_student)

3. Data Types
Every value has a type. Knowing the type is important because it tells Python how to use the value.
1 int → whole numbers (e.g., 10, -5, 0)
2 float → numbers with decimals (e.g., 3.14, -2.5)
3 str → text (e.g., 'Hello')
4 bool → True or False
5 list → collection of items (e.g., [1, 2, 3])
6 tuple → like a list but cannot be changed (e.g., (1, 2, 3))
7 dict → key-value pairs (e.g., {'name': 'Yousuf', 'age': 20})
Example:
x = 10
y = 3.14
z = "Hello"
is_coding_fun = True
numbers = [1, 2, 3, 4]
coordinates = (10, 20)
person = {"name": "Yousuf", "age": 20}
print(x, y, z, is_coding_fun, numbers, coordinates, person)

4. Operators
Operators are symbols that tell Python to perform actions like addition, comparison, or logical
checks.
4.1 Arithmetic Operators
Used for math operations.
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.33 (division)
print(a % b) # 1 (remainder)
print(a ** b) # 1000 (power)
print(a // b) # 3 (floor division)

4.2 Comparison Operators


Used to compare two values. Returns True or False.
x = 5
y = 8
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= 5) # True

4.3 Logical Operators


Used to combine conditions.
a = 10
print(a > 5 and a < 20) # True
print(a > 5 or a < 5) # True
print(not(a > 5)) # False

■ Practice Exercise
1. Create variables a = 15, b = 4.
2. Print the results of:
1 a+b
2 a-b
3 a*b
4 a/b
5 a%b
6 a ** b
By practicing these, you will fully understand how syntax, variables, data types, and operators work
together in Python.

You might also like