notes of python
Python is a versatile, high-level programming language known for its
readability and simplicity
. It is used for web development, data science, automation, machine
learning, and more. Python's clean syntax and extensive standard library
make it accessible for beginners while remaining powerful for experienced
developers.
The basics
"Hello, World!" program: A simple program to get started.
python
print("Hello, World!")
Use code with caution.
Comments: Notes within the code that are ignored by the
interpreter.
o Single-line comments start with # .
o Multi-line comments use triple quotes ( ''' or """ ).
Variables: A variable is a named container for storing data values.
Python is dynamically typed, so you don't need to specify the
variable type when you declare it.
python
name = "Alice" # A string variable
age = 30 # An integer variable
height = 5.9 # A float variable
is_student = True # A boolean variable
Use code with caution.
Data types
Python has several built-in data types.
Numeric types
Integers ( int ): Whole numbers (e.g., 42 , -5 ).
Floats ( float ): Numbers with decimal points (e.g., 3.14 , -0.5 ).
Complex ( complex ): Numbers with a real and an imaginary part
(e.g., 3 + 4j ).
Sequence types
Strings ( str ): Ordered, immutable sequences of characters
enclosed in quotes (e.g., "hello" , 'Python' ).
Lists ( list ): Ordered, mutable collections of items enclosed in
square brackets. Lists can contain items of different data types
(e.g., ["apple", 7, True] ).
Tuples ( tuple ): Ordered, immutable collections of items enclosed
in parentheses (e.g., (10, 20, 30) ).
Other types
Dictionaries ( dict ): Unordered, mutable collections of key-value
pairs enclosed in curly braces (e.g., {"name": "John", "age": 30} ).
Sets ( set ): Unordered collections of unique items enclosed in curly
braces (e.g., {1, 2, 3} ).
Booleans ( bool ): Represents one of two values: True or False .
NoneType : Represents the absence of a value.
Control structures
Control structures determine the order in which code statements are
executed.
Conditional statements
if : Executes a code block if a condition is True .
elif : Checks an additional condition if the initial if statement
is False .
else : Executes a code block if all previous conditions are False .
python
age = 25
if age < 13:
print("You are a child.")
elif age < 20:
print("You are a teenager.")
else:
print("You are an adult.")
Use code with caution.
Loops
Python includes for and while loops for iterating over sequences or
repeating code while a condition is true, respectively. Statements
like break , continue , and pass can modify loop behavior.
Functions and modules
Functions are defined using def for reusable code blocks. Python code
can be organized into modules (files) and accessed using import . The
Standard Library provides built-in modules for common tasks.
Object-Oriented Programming (OOP)
OOP uses classes as blueprints for objects, which are instances of those
classes. Key concepts include constructors ( __init__ ), inheritance,
encapsulation, and polymorphism.
File handling
Python supports reading from and writing to files using
the open() function, often used with a with statement.
Exception handling
Errors can be managed using try , except , and finally blocks to handle
exceptions gracefully.