Python-Haider_Codex
Introduction
Python is one of the most popular dynamically typed Object oriented programing
language which was made by Guido van Russom in 1991. Guido van Russom was
watching a show named Monty Python circus on TV when he was working on this
language. And he thought why not name it as python. And since then its name is
python.
Python Install
Simply install the python on windows and Macs. If python already installed.
Check if you have python installed on a Windows PC, open the cmd or run the
following on the Command Line ([Link]):
C:\Users\Your Name>python --version
Python is an interpreted programming language, this means that as a developer
you write Python (.py) files in a text editor and then put those files into the python
interpreter to be executed.
Let's write our first Python file, called [Link] , which can be done in any text
editor:
# [Link]: File name
print("Welcome Haider_Codex!")
And save this file. Open cmd and run this command
C:\Users\Your Name>python [Link]
#Output "Welcome Haider_Codex!"
Python-Haider_Codex 1
Python Syntax
Python syntax can be executed by writing directly in the Command Line:
print("Welcome Haider_Codex")
Output: Welcome Haider_Codex!
Or creating a python file on the server, using the .py file extension, and running it
in the Command Line:
C:\Users\Your Name>python [Link]
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability
only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.
✅ Example – Correct Indentation:
if 5 > 2:
print("5 is greater than 2")
❌
Wrong Indentation:
if 5 > 2:
print("5 is greater than 2") # ❌ Error: expected an indented block
#Ex-2
if 5 > 2:
print("Five is greater than two!")
Python-Haider_Codex 2
if 5 > 2:
print("Five is greater than two!")
Python Comments
Comments can be used to explain Python code, readable
Creating a Comment
Comments starts with a # , and Python will ignore them:
#This is a comment
print("Welcome Haider_Codex")
# Multiline Comments
#This is a comment
#written in
#more than just one line
print("Haider_Codex!")
"""
This is a comment
written in
more than just one line
"""
print("Haider_Codex!")
Variable
Variable like containers for storing values. eg: Like a box and something putting
into it
x=5
y = "Haider_Codex"
Python-Haider_Codex 3
print(x)
print(y)
name = "Haider"
age = 22
is_student = True
"""
types
string
int
boolean
"""
Rules for Variable Names:
1. Only use letters, numbers, or underscore ( _ )
2. Not start with the number
3. Spaces not allowed
4. Keywords (like if , for , while , etc.) not used
Change Variable Value Anytime:
x=5
print(x) #5
x = "hello"
print(x) # hello
# Python flexible: change the values(means if first use integer and then now
want to use the string values and then you can change this)
Python-Haider_Codex 4
Type Checking
x = 10
print(type(x)) # <class 'int'>
Single or Double Quotes?
String variables can be declared either by using single or double quotes:
x = "Haider"
# is the same as
x = 'Haider'
Case-Sensitive
Variable names are case-sensitive.
a=4
A = "Hadier"
#A will not overwrite a
Type Casting
In python type casting means one data_type conver itno another data_type
like int —> str , str —> int , … etc
x = "123"
y = int(x) # x string tha, ab int ban gaya
print(y + 10) # Output: 133
# e.g
a=7
b = str(a) # a number tha, b string ban gaya
Python-Haider_Codex 5
print("Your score is " + b)
# e.g
z = float(3)
print(z) # Output: 3.0
Python Variables - Assign Multiple Values
Python allows you to assign values to multiple variables in one line:
x, y, z = "Name", "Age", "Address"
print(x)
print(y)
print(z)
"""
One Value to Multiple Variables
assign the same value to multiple variables in one line
"""
x = y = z = "Name"
print(x)
print(y)
print(z)
Unpack
Collection of values in a list, tuple etc. Python allows you to extract the values into
variables. This is called unpacking.
# Example with Tuple:
fruits = ("apple", "banana", "mango")
a, b, c = fruits
print(a) # apple
Python-Haider_Codex 6
print(b) # banana
print(c) # mango
# Example with List:
colors = ["red", "green", "blue"]
x, y, z = colors
print(x) # red
print(y) # green
print(z) # blue
Use of * (Asterisk) in Unpacking
# Agar items zyada hon, toh * ka use karke multiple items ek variable mein le sak
numbers = [1, 2, 3, 4, 5]
a, b, *c = numbers
print(a) # 1
print(b) # 2
print(c) # [3, 4, 5]
Output Variables
The Python print() function is often used to output variables
x = "Python is simple language"
print(x)
# In the print() function, get output multiple variables, separated by a comma:
x = "Python"
Python-Haider_Codex 7
y = "is"
z = "simple"
print(x, y, z)
# You can also use the + operator to output multiple variables:
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
# For numbers, the + character works as a mathematical operator:
x=5
y = 10
print(x + y)
# In the print() function, when you try to combine a string and a number
# with the + operator, Python will give you an error:
# The best way to output multiple variables in the print() function is
# to separate them with commas, which even support different data types:
x=5
y = "John"
print(x, y)
Global Variables
Variables are created outside of a function
Global variables can be used by everyone, both inside of functions and outside
x = "Haider"
def myfunc():
print("Python is " + x)
Python-Haider_Codex 8
myfunc()
# Create a variable inside a function, with the same name as the global variable
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
The global Keyword
To create a global variable inside a function, you can use the global keyword.
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different
things.
Python has the following data types built-in by default, in these categories:
Text Type: str
Python-Haider_Codex 9
Numeric Types: int , float , complex
Sequence Types: list , tuple , range
Mapping Type: dict
Set Types: set , frozenset
Boolean Type: bool
Binary Types: bytes , bytearray , memoryview
None Type: NoneType
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Example Data Type
x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", "banana", "cherry"}) frozenset
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
Python Numbers
Python-Haider_Codex 10
There are three numeric types in Python:
int
float
complex
Variables of numeric types are created when you assign a value to them:
x = 1 # int
y = 2.8 # float
z = 1j # complex
Python-Haider_Codex 11