Python
Python
Python has numerous practical applications across various industries. Some of the top applications include:
Data Science and Analytics: data analysis, machine learning, and visualization
Agentic AI: building autonomous agents, chatbots, and virtual assistants
Machine Learning: developing predictive models and recommender systems
Natural Language Processing (NLP): text analysis, sentiment analysis, and language translation
Computer Vision: image recognition, object detection, and facial recognition
Robotics: building and controlling robots, drones, and autonomous vehicles
Web Development: building web applications and frameworks
Artificial Intelligence and Machine Learning: AI, ML, and deep learning
Automation and Scripting: automating tasks and workflows
Scientific Computing: scientific simulations and data analysis
Cybersecurity: security testing and penetration testing
Internet of Things (IoT): building IoT applications and devices
These applications make Python a versatile and in-demand language in the industry. Its simplicity,
flexibility, and extensive libraries make it a popular choice for developers and data scientists.
Python's simplicity and extensive libraries make it an ideal choice for building Agentic AI applications
that can interact, learn, and adapt to their environments.
addCode
addText
1. Lexical Analysis: The Python code is broken down into individual tokens, such as keywords, identifiers, and
literals.
2. Syntax Analysis: The tokens are analyzed to ensure that the code follows the correct syntax.
3. Semantic Analysis: The code is analyzed to ensure that it makes sense in terms of its meaning and context.
4. Bytecode Generation: The compiled code is generated in the form of bytecode.
[]
import dis
dis.dis(Person)
Disassembly of __init__:
2 0 RESUME 0
3 2 LOAD_FAST 1 (name)
4 LOAD_FAST 0 (self)
6 STORE_ATTR 0 (name)
4 16 LOAD_FAST 2 (age)
18 LOAD_FAST 0 (self)
20 STORE_ATTR 1 (age)
30 LOAD_CONST 0 (None)
32 RETURN_VALUE
Disassembly of greet:
6 0 RESUME 0
This bytecode shows the individual instructions that are executed when the Person class object is created
and __init__ and greet function is called.
Platform Independence: Python bytecode can be executed on any platform that has a Python interpreter, without
the need for recompilation.
Dynamic Typing: Python bytecode is dynamically typed, which means that the type of a variable is determined
at runtime, rather than at compile time.
Flexibility: Python bytecode can be easily modified or extended, which makes it easier to add new features or
functionality to the Python interpreter.
Overall, Python bytecode is an important part of the Python ecosystem, and it plays a key role in making
Python a flexible and platform-independent language.
o Python bytecode is designed to be portable across different OSes (Windows, macOS, Linux,
etc.).
o But it still requires the correct version of the Python interpreter to execute it.
2. Interpreter Dependency:
o Different versions of Python may generate different bytecode.
o A .pyc file created with Python 3.10 might not work in Python 3.8.
3. Machine Independence (Not Fully!):
o While Python bytecode is not tied to a CPU architecture (like x86 or ARM), it still depends on
Python's runtime.
o Some OS-specific modules (like os or sys) might behave differently across platforms.
keyboard_arrow_down
Indentation in Python
Introduction:
In Python, indentation is used to denote a block of code within a control structure, function, or class. It is a
crucial aspect of Python syntax, and incorrect indentation can lead to syntax errors.
What is Indentation?
Indentation is the process of adding spaces or tabs to the beginning of a line of code to indicate that it is part of
a larger block of code. In Python, indentation is used to define the structure of the code and to show the
relationship between different blocks of code.
Why is Indentation Important?
Indentation is important because it helps to:
Define the structure of the code
Show the relationship between different blocks of code
Make the code more readable and understandable
Created by Asifa Majeed
# Incorrect indentation
if True:
print("Hello, World!")
print("This is a block of code")
# Correct indentation
def greet(name: str):
print("Hello, " + name + "!")
# Incorrect indentation
def greet(name: str):
print("Hello, " + name + "!")
Best Practices:
[]
keyboard_arrow_down
[]
Key Characteristics:
1. Dynamically-Typed: Python's data type is determined at runtime, not at compile time.
2. Optional Type Hinting: Type hinting is optional, but it can help with code readability, auto-
completion, and static type checking.
3. No Compile-Time Type Checking: Python does not perform type checking at compile time, but it can
be done using third-party tools or IDEs.
Basic Syntax
The basic syntax for type hinting is to add a colon (:) followed by the expected type after the variable or
function parameter. For example:
x: int = 5
y: str = "hello"
In this example, the greet function takes a name parameter of type str and returns a value of type str.
Type Hinting for Complex Types
You can also use type hinting for more complex types, such as lists, dictionaries, and tuples. For example:
my_list: list[int] = [1, 2, 3]
my_dict: dict[str, int] = {"a": 1, "b": 2}
my_tuple: tuple[str, int] = ("hello", 5)
Best Practices
Use type hints consistently throughout your code.
Use the most specific type possible (e.g., int instead of Any).
Use type hints for function parameters and return types.
Use type hints for complex types, such as lists and dictionaries.
Object-Based Language:
Definition:
An object-based language is a programming language that supports the concept of objects but does not
fully implement all the features of object-oriented programming (OOP). This means that while you can
create and manipulate objects, you may not have full support for features like inheritance,
polymorphism, and encapsulation.
Characteristics:
o Supports objects and encapsulation.
o Does not necessarily support inheritance or polymorphism.
o Examples include JavaScript (prior to ES6), VBScript, and some versions of Pascal.
Use Cases:
Object-based languages are often used for scripting and simple applications where full OOP features are
not required.
Created by Asifa Majeed
Object-Oriented Language:
Definition:
An object-oriented language is a programming language that fully supports the principles of object-
oriented programming, including encapsulation, inheritance, and polymorphism. This allows for more
complex and reusable code structures.
Characteristics:
o Supports encapsulation (hiding data and methods within objects).
o Supports inheritance (creating new classes based on existing ones).
o Supports polymorphism (using a single interface to represent different underlying forms).
o Examples include Python, Java, C++, and Ruby.
Use Cases:
Object-oriented languages are widely used in software development for building large, complex
systems, as they promote code reuse and modularity.
Summary of Differences
Feature Object-Based Language Object-Oriented Language
keyboard_arrow_down
[]
First-Class Functions:
Created by Asifa Majeed
Functions in Python are first-class objects, meaning they can be passed as arguments, returned from
other functions, and assigned to variables. This allows for functional programming paradigms alongside
object-oriented programming.
[]
[]
class Animal:
class Dog(Animal):
return "Woof!"
class Lion(Animal):
return "Roar!"
class Animal:
def speak(self) -> str:
return "Animal speaks"
class Dog(Animal):
def speak(self) -> str:
return "Woof!"
class Lion(Animal):
Created by Asifa Majeed
Duck typing is a fundamental concept in Python programming that enables developers to write flexible and
dynamic code. It's a key aspect of Python's philosophy, which emphasizes "we are all consenting adults here"
and encourages a more relaxed approach to programming.
Here's an example of duck typing using a speaking parrot that talks like a human:
[]
class Human:
Created by Asifa Majeed
def speak(self):
class Parrot:
def speak(self):
person.speak()
human = Human()
parrot = Parrot()
class Human:
def speak(self):
print("Human: I'm good, thanks!")
class Parrot:
def speak(self):
print("Parrot: Polly wants a cracker!")
human = Human()
parrot = Parrot()
[]
class Robot:
def speak(self):
print("Robot: Beep boop, I am functioning within normal parameters!")
robot = Robot()
have_conversation(robot) # Beep boop, I am functioning within normal parameters!
The have_conversation function doesn't need to know anything about the Robot class, because it only cares about
the speak method. This makes it easy to add new types of objects to the conversation, without having to change
the underlying code.