EE Info-Python Notes
EE Info-Python Notes
What is Python?
Features of Python
Applications Of Python
Python Installation
First Python Program
Chapter 1: Introduction
to Python
For Windows:
Visit the official Python website at
www.python.org/downloads/
Developer Tools.
Modules
Built-in Modules:
User-Defined Modules:
Types of Comments
They start with a hash symbol (#) and continue until the
end of the line.
2. Multi-line comments:
Multi-line comments, also known as block comments, allow
you to add comments that span multiple lines.
Valid Characters:
2. Dictionary:
Dictionaries are key-value pairs enclosed in curly braces.
Each value is associated with a unique key, allowing for
efficient lookup and retrieval. For example, person =
{'name': 'John', 'age': 25, 'city': 'New York'}.
3. Boolean:
Boolean (bool): Booleans represent truth values, either True
or False. They are used for logical operations and
conditions. For example, is_valid = True.
4. Set:
Sets (set): Sets are unordered collections of unique
elements enclosed in curly braces. They are useful for
mathematical operations such as union, intersection,
and difference. For example, fruits = {'apple', 'banana',
'orange'}.
5. Sequence Type:
Sequences represent a collection of elements and include
data types like strings, lists, and tuples. Strings are used to
store textual data, while lists and tuples are used to store
ordered collections of items.
Arithmetic operators
Comparison operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic Operators
Arithmetic Operators:
Arithmetic operators in Python are used to perform
mathematical calculations on numeric values. The basic
arithmetic operators include:
Addition (+): Adds two operands together. For example, if
we have a = 10 and b = 10, then a + b equals 20.
Comparison Operators:
Comparison operators in Python are used to compare two
values and return a Boolean value (True or False) based on
the comparison. Common comparison operators include:
Equal to (==): Checks if two operands are equal.
Less than (<): Checks if the left operand is less than the
right operand.
Assignment Operators:
Assignment operators are used to assign values to variables.
They include:
Equal to (=): Assigns the value on the right to the variable
on the left.
Logical Operators:
Logical operators in Python are used to perform logical
operations on Boolean values. The main logical operators
are:
Logical AND (and): Returns True if both operands are
True, otherwise False.
Logical OR (or): Returns True if at least one of the
operands is True, otherwise False.
Bitwise Operators:
Bitwise operators perform operations on individual bits of
binary numbers. Some common bitwise operators in Python
are:
Bitwise AND (&): Performs a bitwise AND operation on the
binary representations of the operands.
Right shift (>>): Shifts the bits of the left operand to the
right by the number of positions specified by the right
operand.
Right shift (>>): Shifts the bits of the left operand to the
right by the number of positions specified by the right
operand.
Membership Operators:
Membership operators are used to test whether a value is a
member of a sequence (e.g., string, list, tuple). They include:
In: Returns True if the value is found in the sequence.
Identity Operators:
Identity operators are used to compare the identity of two
objects. They include:
Is: Returns True if both operands refer to the same object.
Control Flow
Control flow directs program execution through structures
like loops, conditionals, and functions, determining the order
and path of operations.
If statements
An `if` statement in Python checks whether a condition is
true or false. If the condition is true, the code inside the `if`
block runs. If false, the code is skipped. It's used to make
decisions in the program, executing specific actions based
on conditions
example:
Else and elif statements
In Python, else and elif statements are used alongside if to
handle multiple conditions and alternative actions.
example:
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Nested if Statement
If the weather isn’t sunny, you skip the nested checks and
go straight to an alternative action, like staying indoors.
example:
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
The While Loop
For example
A `while` loop can keep counting up as long as the count is
below a certain number.
For example
It can go through a list of numbers, processing each one in
turn. It's ideal for repetitive tasks like iterating over data
collections.
example:
example:
example:
Using Range() in For Loop
example:
Today’s Topics:
list and Methods
Indexing and Slicing
List Comprehension
Tuples
Tuples packing,and unpacking
Lists and Tuples
Lists and tuples in Python both store collections of items.
Lists are mutable, allowing changes like adding or removing
elements, and use square brackets (`[ ]`).
Key Features:
1. append()
Adds a single element to the end of the list.
Syntax: list.append(element)
2. extend()
Extends the list by appending elements from an iterable
(like another list).
Syntax: list.extend(iterable)
3. insert()
Inserts an element at a specified position in the list.
Syntax: list.insert(index, element)
4. remove()
Removes the first occurrence of a specified element from
the list.
Syntax: list.remove(element)
5.pop()
Removes and returns the element at a specified position
(index). If no index is specified, it removes and returns
the last element.
Syntax: list.pop(index)
Indexing
Key Points:
1.Positive Indexing:
3. Indexing in Strings:
Slicing
List Comprehension
Syntax:
expression: The value or operation applied to each item.
item: The variable representing the current element in the
iteration.
iterable: The collection (like a list, tuple, or range) that you
are iterating over.
condition: (Optional) A filter that decides whether the
expression should be applied to the current item.
Tuple Unpacking:
Tuple unpacking is the reverse process, where the values in a
tuple are extracted and assigned to individual variables.
Dictionaries
A dictionary in Python is a collection of key-value pairs
where each key is unique and is used to retrieve the
corresponding value.
Dictionaries Methods
1. dict.get()
The get() method retrieves the value for a specified key. If the
key is not found, it returns None or a specified default value.
examples:
2. dict.keys()
The keys() method returns a view object that displays a list
of all the keys in the dictionary.
m.
examples:
3. dict.values()
The values() method returns a view object that displays a
list of all the values in the dictionary.
examples:
4. dict.items()
The items() method returns a view object that displays a list
of tuples, where each tuple is a key-value pair.
examples:
5. dict.update()
The update() method updates the dictionary with elements
from another dictionary or iterable of key-value pairs. If a
key already exists, its value will be updated.
examples:
6. dict.pop()
The pop() method removes a specified key and returns its
corresponding value. If the key is not found, it raises a
KeyError, unless a default value is provided.
examples:
7. dict.popitem()
The popitem() method removes and returns a (key, value)
pair as a tuple.
examples:
8. dict.clear()
The clear() method removes all items from the dictionary,
leaving it empty.
examples:
9. dict.copy()
The copy() method returns a shallow copy of the dictionary.
examples: