unit-1 python
unit-1 python
Python Versions
Python Installation
Environmental Variables
1. Access: Control Panel > System > Advanced System Settings > Environment Variables.
2. Add Python Path: Add the path to the Python executable to the Path variable.
Example:
python --version
Getting Help
Example:
help(print)
Dynamic Typing
x = 10
Naming Conventions
Rules:
student_name = "John"
_age = 21
Character Set
Comments
Types:
Identifiers
Rules:
Start with a letter or _.
Data Types
Text: str
Boolean: bool
Mapping: dict
Example:
x = 10 # int
y = 3.14 # float
z = "Hello" # str
Operators
Arithmetic: +, -, *, /
Type Conversions
Example:
x = "123"
print(int(x) + 1)
String Methods
Common Methods:
Example:
print(name.strip())
name = "John"
age = 25
print("My name is {} and I am {} years old".format(name, age))
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks) that is
used to store an ordered collection of items. We can store all types of items (including
another list) in a list. A list may contain mixed type of items, this is possible because a list
mainly stores references at contiguous locations and actual items maybe stored at different
locations.
Creating a List
# List of integers
a = [1, 2, 3, 4, 5]
# List of strings
print(a)
print(b)
print(c)
Output
[1, 2, 3, 4, 5]
We can also create a list by passing an iterable (like a string, tuple, or another list) to
the list() function.
# From a tuple
Python
pop(): Removes the element at a specific index or the last element if no index is specified.
Python
We can iterate the Lists easily by using a for loop or other iteration methods. Iterating over
lists is useful when we want to do some operation on each item or access specific items
based on certain conditions. Let’s take an example to iterate over the list using for loop.
for item in a:
print(item)
Tuples in Python
# Note : In case of list, we use square # brackets []. Here we use round brackets ()
print(type(t))
Unlike Python lists, tuples are immutable. Some Characteristics of Tuples in Python.
Like Lists, tuples are ordered and we can access their elements using their index values
t = (1, 2, 3, 4, 5)
print(t[1])
print(t[4])
t = (1, 2, 3, 4, 2, 3)
print(t)
# updating an element
t[1] = 100
print(t)
Traversing
Concatenation
Nesting
Repetition
Slicing
Deleting
Tuples in a Loop
Without Brackets
Tuple Constructor
Empty Tuple
Sets in Python
A Set in Python programming is an unordered collection data type that is iterable and has no
duplicate elements. While sets are mutable, meaning you can add or remove elements after
their creation, the individual elements within the set must be immutable and cannot be
changed directly.
The major advantage of using a set, as opposed to a list, is that it has a highly optimized
method for checking whether a specific element is contained in the set. This is based on a
data structure known as a hash table. Since sets are unordered, we cannot access items
using indexes as we do in lists.
type(var)
print(myset)
myset.add("d")
print(myset)
Frozen sets in Python are immutable objects that only support methods and operators that
produce a result without affecting the frozen set or sets to which they are applied. It can be
done with frozenset() method in Python.
While elements of a set can be modified at any time, elements of the frozen set remain the
same after creation.
print("Normal Set")
print(normal_set)
# A frozen set
print("\nFrozen Set")
print(frozen_set)
Insertion in the set is done through the set.add() function, where an appropriate record
value is created to store in the hash table. Same as checking for an item, i.e., O(1) on
average. However, in worst case it can become O(n).
Union operation on Python Sets-Two sets can be merged using union() function or |
operator. Both Hash Table values are accessed and traversed with merge operation perform
on them to combine the elements, at the same time duplicates are removed. The Time
Complexity of this is O(len(s1) + len(s2)) where s1 and s2 are two sets whose union needs to
be done.
Intersection operation on Python Sets- This can be done through intersection() or &
operator. Common Elements are selected. They are similar to iteration over the Hash lists
and combining the same values on both the Table. Time Complexity of this is O(min(len(s1),
len(s2)) where s1 and s2 are two sets whose union needs to be done.
Operators Notes
s1 == s2 s1 is equivalent to s2
s1 != s2 s1 is not equivalent to s2
s1 <= s2 s1 is subset of s2
s1 >= s2 s1 is superset of s2
Dictionaries in Python
A Python dictionary is a data structure that stores the value in key: value pairs. Values in a
dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated
and must be immutable.
Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to
find values.
Python dictionaries are essential for efficient data mapping and manipulation in
programming. To deepen your understanding of dictionaries and explore advanced
techniques in data handling, consider enrolling in our Complete Machine Learning & Data
Science Program. This course covers everything from basic dictionary operations to
advanced data processing methods, empowering you to become proficient in Python
programming and data analysis.
Create a Dictionary
print(d1)
print(d2)
We can access a value from a dictionary by using the key within square brackets
or get() method.
We can add new key-value pairs or update existing keys by using assignment.
We can iterate over keys [using keys() method] , values [using values() method] or both
[using item() method] with a for loop.
print(key)
print(value)
print(f"{key}: {value}")