Day 01
Introduction to Python

Python is a high-level, interpreted programming language known for its clear syntax and readability. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace.
Its versatile nature allows Python to be used in various domains, such as web development, data analysis, artificial intelligence, machine learning, automation, and scientific computing. Companies like Google, Netflix, and Facebook use Python for a wide range of purposes, demonstrating its adaptability and power.
With a rich ecosystem of libraries and frameworks, Python makes it possible to work on complex tasks with relatively simple and understandable code, making it a popular choice for beginners and experts alike.
You can also download the Complete Day 01 Notes from Here
Features of Python
Python is known for its numerous appealing features which contribute to its popularity among programmers. Below are some of the most notable features:
- Ease of Learning and Readability: Python's syntax is clear and intuitive, making it an ideal choice for beginners.
- Expressive: More functionality can be achieved with fewer lines of code.
- Interpreted: Python code is executed line by line, which makes debugging easier.
- Cross-platform: It runs on multiple operating systems like Windows, macOS, and Linux.
- Open Source: Python is freely available and can be distributed and modified, benefiting from community contributions.
- Extensive Libraries: A vast standard library provides modules for numerous functionalities.
- Object-Oriented: Python supports OOP with classes and objects.
- Integration: It can be integrated with languages like C and C++.
- Dynamic Typing: Python does dynamic type checking.
- Extensibility: Programmers can add low-level modules to the Python interpreter.
- Database Connectivity: It supports interaction with most databases.
- Automatic Memory Management: Features garbage collection to manage memory usage.
First Python Program
Python is known for its straightforward syntax that is similar to the English language. Here's how you can write and run your first Python program.
Writing Your First Program
Create a new text file with the .py extension, for example, hello.py. Then, open it in a text editor and write your first program:
This program uses the print() function to send the output to the screen.
Running Your First Program
To run your Python program, open your command prompt or terminal, navigate to the directory containing your hello.py file, and then type:
If Python is installed correctly and the PATH is set, you should see the following output:
Success
Congratulations! You've just written and run your first Python program.
In Python, the basic syntax is simple and clean, which makes the code very easy to read and write. Here's an overview of the basic syntax, variables, and data types in Python:
Basic Syntax
- Comments: Use the hash symbol (#) for single-line comments. For multi-line comments, you can use triple quotes (''' or """).
- Indentation: Python uses indentation to define blocks of code. The standard practice is to use 4 spaces per indentation level.
- Case Sensitivity: Python is case-sensitive. For example, Variable and variable are two different identifiers.
- Statements: Instructions that a Python interpreter can execute are called statements. For example, print("Hello, world!") is a statement.
For Example,
Variables
- Declaration: Variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. E.g., x = 10.
- Dynamic Typing: Python is dynamically-typed, which means you can reassign variables to different data types. E.g., x = "Hello" is valid even after x = 10.
- Immutable Variables: Some of Python's internal data types, like strings and numbers, are immutable, meaning their contents cannot be changed after they are created.
For Example,
Data Types
Primitive Types
- Numbers: Integers (int), floating-point numbers (float), and complex numbers (complex).
- Strings: Ordered sequences of characters, e.g., "Hello, world!".
- Booleans: Represents True or False values.
Data Structures
- Lists: Ordered and mutable collections, e.g., [1, 2, 3].
- Tuples: Ordered and immutable collections, e.g., (1, 2, 3).
- Dictionaries: Unordered and mutable collections of key-value pairs, e.g., {"name": "Alice", "age": 25}.
- Sets: Unordered collections of unique elements, e.g., {1, 2, 3}.
Info
To get the type of a Variable or a Data Structure in Python We can use type(var) Inbuilt Function
For Example,
These are the fundamental elements you'll use when starting to code in Python. Remember, Python is designed to be easy to understand and fun to use, which is why its syntax is often considered one of its greatest strengths.
Operators in Python
Python includes several types of operators:
Arithmetic Operators
Perform mathematical operations like addition (+), subtraction (-), multiplication (), division (/), modulus (%), exponentiation (*), and floor division (//).
For Example,
Comparison Operators
Compare values and include equals (==), not equals (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
For Example,
Logical Operators
Used for logical operations, primarily with boolean values, including and, or, and not.
For Example,
Assignment Operators
Assign values to variables, e.g., =, +=, -=, *=, /=, etc.
For Example,
Identity Operators
is and is not check if two variables refer to the same object in memory.
For Example,
Membership Operators
in and not in check for membership in a sequence, such as lists or strings.
For Example,
Bitwise Operators
Perform bitwise calculations on integers, including & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift).
For Example,
Functions in Python
A function is a block of organized, reusable code that is used to perform a single, related action. Functions help break our program into smaller and modular chunks.
Defining a function:
Calling a function:
About Functions
We will dive Deep into functions in later classes
Modules
A module is a collection of Functions in a Single Python file.
It may also contain classes, and variables, and is used to organize related code. A module is a Python file with a .py extension.
You can use any Python source file as a module by executing an import statement in another Python source file.
Packages
Packages are collections of modules.
Packages are a way of structuring Python’s module namespace by using “dotted module names.” A package is a directory containing a special file __init__.py and can contain subpackages and modules.
Creating Modules in Python
Step 0: Create the directory called (pack)
Navigate to the directory and follow the further steps.
Step 1: Create the first module (modone.py)
In this module, it consists of two functions, Addition and Multiplication.
Step 2: Create the second module (modtwo.py)
In this module, it consists of two functions, Subtraction and Division.
Step 3: Create a Script to Use the Modules (callmod.py)
Step 4: Run Your Script (callmod.py)
Ensure your file are structured as follows in the same directory:
Creating Packages in Python
Step 5: Create an Empty __init__.py file in the pack directory
Your directory structure should now look like this:
Step 6: Use the Package in Your Script (callpack.py)
Your directory structure should now look like this at the End:
.
├── Project Directory
├── pack/
│ ├── __init___.py
│ ├── modone.py
│ ├── modtwo.py
│ └── callmod.py
└── callpack.py
Rules of Python - PEP8 (Python Style Guide)
PEP 8, or Python Enhancement Proposal 8, is the official style guide for the Python code comprising the conventions that Python developers are advised to follow. Here are some key aspects of PEP 8:
-
Indentation: Use 4 spaces per indentation level. Continuation lines should align wrapped elements either vertically, or using a hanging indent of 4 spaces.
-
Maximum Line Length: Limit all lines to a maximum of 79 characters for code and 72 characters for comments and docstrings.
-
Blank Lines: Use blank lines to separate functions and classes, and larger blocks of code inside functions.
-
Whitespace in Expressions and Statements : Avoid extraneous whitespace in the following situations:
- Immediately inside parentheses, brackets, or braces.
- Between a trailing comma and a following close parenthesis.
- Immediately before a comma, semicolon, or colon.
- However, use whitespace around arithmetic operators.
-
Comments: Comments should be complete sentences and should be used sparingly, i.e., only when necessary to explain complex pieces of code.
-
Naming Conventions:
- Functions: Function names should be lowercase, with words separated by underscores as necessary to improve readability.
- Variables: Use a lowercase single letter, word, or words. Separate words with underscores to improve readability.
- Classes: Class names should follow the UpperCaseCamelCase convention.
- Constants: Constants are usually defined on a module level and written in all capital letters with underscores separating words.
PEP 8 is a guideline, not a strict set of rules. It's encouraged to adhere to it, but there can be exceptions based on the context and necessity.