Python Introduction
* Python is a powerful and beginner-friendly programming language created by
Guido van Rossum in 1989 and officially released in 1991.
* Guido developed Python during his free time over the holidays while working at
a research institute in the Netherlands.
* The name "Python" doesn't come from the snake but from Guido's favorite
comedy show, "Monty Python's Flying Circus".
* He wanted the language to be fun and easy to use, just like the show.
Python version :
* Python became popular because of its simple and readable syntax, which looks
a lot like regular English.
* Over time, it has evolved through various versions. The first version, Python
1.0, was released with basic features like functions and exception handling.
* In 2000, Python 2.0 introduced advanced tools like list comprehensions and
automatic memory cleanup.
* Later, in 2008, Python 3.0 made the language cleaner and more efficient,
though it wasn’t backward-compatible with older versions.
* Today, Python is one of the most widely used programming languages in the
world
Applications of Python:
* Python is incredibly versatile and is used in many fields.
* It is popular for web development, where frameworks like Django and Flask
help build websites.
* In data science and machine learning, Python is the top choice, with
powerful libraries like Pandas and TensorFlow.
* It is also used for automation, helping to write scripts that can save time by
performing repetitive tasks automatically.
* Python is even used in game development, desktop applications, and IoT
(Internet of Things), making it a tool that fits almost every need.
IDE Tools to use Python:
* IDE is Integrated Development Environment.
* It is a software application that provides tools like a code editor, debugger,
and compiler/interpreter in one interface, making programming easier
and more efficient.
Python IDE Tools:
* PyCharm
* Visual Studio Code (VS Code)
* Jupyter Notebook
* IDLE
* Thonny
* Spyder
* Atom
* Sublime Text
* Anaconda
* Wing IDE
Python Scripting
* Python scripting refers to writing Python code that automates tasks, processes
data, or performs specific actions.
* Unlike full-fledged software development, scripting focuses on creating scripts
—short programs meant to execute a series of instructions or automate a task
efficiently.
Common Applications of Python Scripting
1. System Administration:
Automating file operations, running system commands, or managing
servers. Example: Renaming multiple files in a folder.
2. Data Processing:
Cleaning, transforming, and analyzing data. Example: Reading CSV files,
applying calculations, and saving results.
3. Web Scraping:
Extracting data from websites. Example: Scraping product prices from e-
commerce sites.
4. API Interaction:
Automating API calls for data retrieval or updates. Example: Fetching
weather data from an API.
5. Testing and Debugging:
Writing test cases for software and debugging issues.
6. Task Automation:
Scheduling and running periodic tasks. Example: Sending automated
email reminders.
7. Machine Learning and AI:
Running training scripts for models. Example: Automating data
preprocessing and model evaluation.
Python Interactive Mode (REPL)
1. R: Read – It reads the code or input you type.
2. E: Evaluate – It evaluates the code or expression.
3. P: Print – It displays the result of the evaluation.
4. L: Loop – It goes back to accept more input in a loop.
Variables
What is variables?
Variable are the containers which holds the data.
Ways of writting variables:
1. Snake case : This is the most commonly used convention in Python. Words
are separated by underscores, and all letters are lowercase.
Example:
2.
3. my_variable_name = 10
4. first_name = "Vishwanath"
5.
6. Camel Case: This is less common in Python but still used by some
developers. The first word is lowercase, and each subsequent word starts
with an uppercase letter.
Example:
7.
8. myVariableName = 10
firstName = "Vishwanath"
9. Single Underscore Prefix (For Internal Use):Used to indicate that a variable
is intended to be private.
Example:
10.
11. _temp_value = 50
12.
[Link] Underscore Prefix (For Name Mangling in Classes):Used to avoid
name conflicts in subclasses.
Example:
14.
15. class Sample:
16. def __init__(self):
17. self.__hidden_var = 10 # Name-mangled to _Sample__hidden_var
18.
[Link] Leading & Trailing Underscores (For Avoiding Conflicts with
Keywords):Helps avoid clashes with built-in names.
Example:
class_ = "Math" # Avoids conflict with the `class` keyword
[Link] Case: Similar to camel case, but the first word also starts with an
uppercase letter. This is usually used for class names rather than
variables.
Example:
21. MyVariableName = 10
FirstName = "Vishwanath"
[Link] Case (Screaming Snake Case):
Example:
23. MY_VARIABLE_NAME = "Data Analyst"
24. USER_AGE = 45
25. ID=EP001
26.
[Link] vs Local Variables
1. A global variable is a variable that is declared outside of any
function, class, or method and can be accessed and modified from
anywhere in the program.
2. A local variable, on the other hand, is a variable that is declared
within a function or block of code and is only accessible within that
function or block.
Example:
variable = 10 # Global variable
def function():
value = 5 # Local variable
global variable # Declaring global variable
variable += value # Using the global variable
print(f"Local variable: {value}")
print(f"Global variable: {variable}")
function()
Rules for Naming Variables:
Start with a Letter or Underscore
Followed by Letters, Digits, or Underscores
Case-Sensitive
No Spaces
No Reserved Keywords
Python Data Type
There are two types of data types in python mentioned below:
1. Primitive Data Types:
Primitive data types are the most basic data types available within a
programming language. These types are predefined by the language and are
used to represent simple values.
1. Integer (int): Represents whole numbers without a fractional part.
2. Floating-point (float): Represents numbers with a fractional part
(decimals).
3. Character (char): Represents a single character (e.g., 'a', 'b', '1', '$').
4. Boolean (bool): Represents true or false values (True or False in Python)
In Python, these include:
int: For integers.
float: For floating-point numbers.
bool: For boolean values.
str: For strings (though in some languages, str is not considered a primitive
type).
2. Non-Primitive Data Types:
Non-primitive data types are more complex and are derived from primitive data
types. They are used to store multiple values or collections of values.
In Python, non-primitive data types include:
1. list: Ordered and mutable sequences of elements.
2. tuple: Ordered and immutable sequences of elements.
3. set: Unordered and mutable collections of unique elements.
4. frozenset: Unordered and immutable collections of unique elements.
5. dict: Collections of key-value pairs.
Comment Lines in Python
Comment lines are lines of text in a code file that the Python interpreter ignores.
They are used to explain and annotate the code, making it more understandable
for anyone reading it, including your future self.
Why we need to use Comment lines?
1. Clarify Code Functionality:Explain what specific parts of the code are
doing.
2. Provide Context: Offer background information or the purpose of certain
sections of the code.
3. Improve Readability: Make the code more readable and easier to follow.
4. Debugging: Temporarily disable certain parts of the code without deleting
them.
Types of Comments in Python
1. Single-line Comments ===> #
example:
2. Multi-line comments ===> """ or '''
Python Data Type
There are two types of data types in python mentioned below:
1. Primitive Data Types:
Primitive data types are the most basic data types available within a
programming language. These types are predefined by the language and are
used to represent simple values.
1. Integer (int): Represents whole numbers without a fractional part.
2. Floating-point (float): Represents numbers with a fractional part
(decimals).
3. Character (char): Represents a single character (e.g., 'a', 'b', '1', '$').
4. Boolean (bool): Represents true or false values (True or False in Python)
In Python, these include:
int: For integers.
float: For floating-point numbers.
bool: For boolean values.
str: For strings (though in some languages, str is not considered a primitive
type).
2. Non-Primitive Data Types:
Non-primitive data types are more complex and are derived from primitive data
types. They are used to store multiple values or collections of values.
In Python, non-primitive data types include:
1. list: Ordered and mutable sequences of elements.
2. tuple: Ordered and immutable sequences of elements.
3. set: Unordered and mutable collections of unique elements.
4. frozenset: Unordered and immutable collections of unique elements.
5. dict: Collections of key-value pairs.
Comment Lines in Python
Comment lines are lines of text in a code file that the Python interpreter ignores.
They are used to explain and annotate the code, making it more understandable
for anyone reading it, including your future self.
Why we need to use Comment lines?
1. Clarify Code Functionality:Explain what specific parts of the code are
doing.
2. Provide Context: Offer background information or the purpose of certain
sections of the code.
3. Improve Readability: Make the code more readable and easier to follow.
4. Debugging: Temporarily disable certain parts of the code without deleting
them.
Types of Comments in Python
1. Single-line Comments ===> #
example:
2. Multi-line comments ===> """ or '''