Python Training
Python Training
Programming
www.bahwancybertek.com
© Bahwan CyberTek
History of Python and Who Invented Python?
© Bahwan CyberTek 3
Install python on windows machine
https://www.python.org/downloads/
© Bahwan CyberTek 4
Install python on windows machine continue …..
© Bahwan CyberTek 5
Click Customize installation
© Bahwan CyberTek 6
Click next
Before that Create a folder ”Python” in D
drive for python installation . Brows and select the
installation directory
© Bahwan CyberTek 7
Click install to install python in the selected folder
© Bahwan CyberTek 8
© Bahwan CyberTek 9
Verify python installation
Open command prompt and type python --version
© Bahwan CyberTek 1
0
To run python script in command prompt
type python and enter , you will get python command execution
prompt where you can type and execute your script. (type clear
to clear the screen)
© Bahwan CyberTek 1
1
Python Variable
• Python Variable is containers that store values. Python is not “statically typed”.
An Example of a Variable in Python is a representational name that serves as a
pointer to an object. Once an object is assigned to a variable, it can be referred
to by that name
Rules for Python variables
A Python variable name must start with a letter or the underscore character.
A Python variable name cannot start with a number.
A Python variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ ).
Variable in Python names are case-sensitive (name, Name, and NAME are three
different variables).
The reserved words(keywords) in Python cannot be used to name the variable in
Python
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
© Bahwan CyberTek 1
2
Python Data Types
© Bahwan CyberTek 1
3
Description of the data types
© Bahwan CyberTek 1
5
Python Input/Output
• This function first takes the input from the user and
converts it into a string. The type of the returned
object always will be <class ‘str’>. It does not
evaluate the expression it just returns the complete
statement as String, and will print it.
• Example :
• # Python program show input and Output
• val = input("Enter your value: ")
• print(val)
© Bahwan CyberTek 1
6
Python Operators
© Bahwan CyberTek 1
7
Python if-elif-else ladder
i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
© Bahwan CyberTek 1
8
Python For Loop and Python While Loop
• Python For loop is used for sequential traversal i.e. it is used for
iterating over an iterable like String, Tuple, List, Set, or Dictionary.
Here we will see a “for” loop in conjunction with the range() function
to generate a sequence of numbers starting from 0, up to (but not
including) 10, and with a step size of 2. For each number in the
sequence, the loop prints its value using the print() function.
# Python program to illustrate for loop
© Bahwan CyberTek 1
9
Python Functions
© Bahwan CyberTek 2
0
Cont…
Python classes and objects are the starting point in Python Programming.
A class in Python is a blueprint for an object and the object in Python is an
instance of it but with real values. They make the code more organized and
structured
Define Python Class
class ClassName:
# class definition
Ex:
class Bike:
name = ""
gear = 0
Here,
Bike - the name of the class
name/gear - variables inside the class with default values "" and 0
respectively.
Note: The variables inside a class are called attributes.
© Bahwan CyberTek 2
2
Python Objects
Example :
# create class
class Bike:
name = ""
gear = 0
Here, bike1 is the object of the class. Now, we can use this object to access the
class attributes.
© Bahwan CyberTek 2
3
Access Class Attributes Using Objects
© Bahwan CyberTek 2
4
Python Constructors
© Bahwan CyberTek 2
5
Python Methods
# create a class
class Room:
length = 0.0
breadth = 0.0
# method to calculate area
def calculate_area(self):
print("Area of Room =", self.length * self.breadth)
© Bahwan CyberTek 2
6
Encapsulation in Python
© Bahwan CyberTek 2
7
Python Encapsulation -Examples
Public :
class MyClass:
def __init__(self, value):
self.value = value # Public attribute
def show_value(self): # Public method
print(self.value)
# Usage
obj = MyClass(10)
obj.show_value() # Accessing public method
print(obj.value) # Accessing public attribute
© Bahwan CyberTek 2
8
Private Access Modifier
class MyClass:
def __init__(self, value):
self.__value = value # Private attribute
def __show_value(self): # Private method
print(self.__value)
def public_method(self): # Public method
self.__show_value() # Accessing private method within the
class
# Usage
obj = MyClass(10)
# obj.__show_value() # This would raise an AttributeError
# print(obj.__value) # This would raise an AttributeError
obj.public_method() # Accesses private method through a
public method
© Bahwan CyberTek 2
9
Protected Access Modifier
class BaseClass:
def __init__(self, value):
self._value = value # Protected attribute
def _show_value(self): # Protected method
print(self._value)
class SubClass(BaseClass):
def display(self):
self._show_value() # Accessing protected method from the
subclass
# Usage
obj = SubClass(10)
obj.display() # Accessing protected method via public method
in subclass
print(obj._value) # Accessing protected attribute directly (not
recommended)
© Bahwan CyberTek 3
0
Python Inheritance
© Bahwan CyberTek 3
1
Inheritance Example
class Animal:
# attribute and method of the parent class
name = ""
def eat(self):
print("I can eat")
# inherit from Animal
class Dog(Animal):
# new method in subclass
def display(self):
# access name attribute of superclass using self
print("My name is ", self.name)
# create an object of the subclass
labrador = Dog()
# access superclass attribute and method
labrador.name = "Rohu"
labrador.eat()
# call subclass method
labrador.display()
© Bahwan CyberTek 3
2
Method Overriding in Python Inheritance
same method (function) in the subclass overrides the method in the superclass.
if we need to access the superclass method from the subclass, we use the
super() function
class Animal:
name = ""
def eat(self):
print("I can eat")
# inherit from Animal
class Dog(Animal):
# override eat() method
def eat(self):
# call the eat() method of the superclass using super()
super().eat()
print("I like to eat bones")
# create an object of the subclass
labrador = Dog()
labrador.eat()
© Bahwan CyberTek 3
4
Abstract Classes in Python
In Python, an abstract class is a class that cannot be instantiated on its own and
is designed to be a blueprint for other classes. Abstract classes allow us to define
methods that must be implemented by subclasses, ensuring a consistent
interface while still allowing the subclasses to provide specific implementations.
Example :
# Define an abstract class
class Animal(ABC):
@abstractmethod
def sound(self):
pass # This is an abstract method, no implementation here.
# Concrete subclass of Animal
class Dog(Animal):
def sound(self):
return "Bark" # Providing the implementation of the abstract method
# Create an instance of Dog
dog = Dog()
print(dog.sound()) # Output: Bark
© Bahwan CyberTek 3
5
Defining REST
© Bahwan CyberTek 3
6
HTTP response status codes
© Bahwan CyberTek 3
7
To work with Rest API we need Request package
installed on client machine .Follow the below steps to
install the package
© Bahwan CyberTek 3
8
POST Example & Get Request
Post Example :
import requests
api_url = "http://localhost:8080/api/tutorials"
tutorials= {'title': 'H2 DB Complete Guide', 'description': 'Python
Example program'}
response = requests.post(api_url, json=tutorials)
print(response.json())
print(response.status_code)
Get Example:
import requests
api_url = "http://localhost:8080/api/tutorials"
response = requests.get(api_url)
#response.json()
print(response.json())
© Bahwan CyberTek 3
9
PUT and DELETE Request
import requests
api_url = "http://localhost:8080/api/tutorials/6352"
tutorials= {'id': 6352, 'title': 'Python Complete Complete Guide',
'description': 'Python Example program', 'published': True}
response = requests.put(api_url, json=tutorials)
print(response.json())
print(response.status_code)
print("****************************
*********");
print("**************************** calll get request to see the test
reqult *********");
api_url = "http://localhost:8080/api/tutorials"
response = requests.get(api_url)
#response.json()
print(response.json())
print(response.status_code)
© Bahwan CyberTek 4
0
Delete Request
import requests
api_url = "http://localhost:8080/api/tutorials/6352"
response = requests.delete(api_url)
print(response.status_code)
© Bahwan CyberTek 4
1
PATCH Request
import requests
api_url = "http://localhost:8080/api/tutorials/6352"
tutorials= {'title': 'Mastering Python Program '}
response = requests.patch(api_url, json=tutorials)
print(response.json())
print(response.status_code)
print("**************************** *********");
print("**************************** calll get request to see the test
reqult *********");
api_url = "http://localhost:8080/api/tutorials"
response = requests.get(api_url)
#response.json()
print(response.json())
print(response.status_code)
© Bahwan CyberTek 4
2
© Bahwan CyberTek 4
3