0% found this document useful (0 votes)
0 views

Python Part 2

The document provides an introduction to various data structures in Python, including lists, dictionaries, tuples, and sets, highlighting their characteristics and usage. It also covers functions, lambda functions, and recursion, explaining their definitions and providing code examples. Additionally, it discusses function scope and lifetime, emphasizing the visibility and memory duration of variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Part 2

The document provides an introduction to various data structures in Python, including lists, dictionaries, tuples, and sets, highlighting their characteristics and usage. It also covers functions, lambda functions, and recursion, explaining their definitions and providing code examples. Additionally, it discusses function scope and lifetime, emphasizing the visibility and memory duration of variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Introduction to Python

Part 2
List
Python Lists are just like the arrays, declared in other languages which is an
ordered collection of data. It is very flexible as the items in a list do not need to be
of the same type

List = [1, 2, 3, "aa", 2.3]


print(List)

Output
[1, 2, 3, ‘aa’, 2.3]
Arrays in Python

Python does not have built-in support for Arrays, but Python Lists can be used
instead.
Built in methods in list/array
Strings in Python
A sequence of characters enclosed in quotes.

text = "Hello, Python!"

print(text[0:5])
Dictionary
Python dictionary is like hash tables in any other language with the time
complexity of O(1).

It is an unordered collection of data value.

It is used to store data values like a map, which, unlike other Data Types that hold
only a single value as an element, Dictionary holds the key:value pair.

Key-value is provided in the dictionary to make it more optimized.


Dictionary example
# Creating a Dictionary
# accessing a element using get()
Dict = {'Name': 'a', 1: [1, 2, 3, 4]}
# method
print("Creating Dictionary: ")
print("Accessing a element using get:")
print(Dict)
print(Dict.get(1))

# accessing a element using key # creation using Dictionary comprehension

print("Accessing a element using key:") myDict = {x: x**2 for x in [1,2,3,4,5]}


print(Dict['Name']) print(myDict) Output
Creating Dictionary:
{'Name': 'a', 1: [1, 2, 3, 4]}
Accessing a element using key:
a
Accessing a element using get:
[1, 2, 3, 4]
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Tuple

● Python Tuple is a collection of Python objects much like a list but Tuples are
immutable in nature i.e. the elements in the tuple cannot be added or
removed once created.
● Just like a List, a Tuple can also contain elements of various types.
● In Python, tuples are created by placing a sequence of values separated by
‘comma’ with or without the use of parentheses for grouping of the data
sequence.
● Note: Tuples can also be created with a single element, but it is a bit tricky.
Having one element in the parentheses is not sufficient, there must be a
trailing ‘comma’ to make it a tuple.
TUPLE:
# Accessing element using indexing
# Creating a Tuple with print("First element of tuple")
# the use of Strings print(Tuple[0]) Output
Tuple with the use of String:
Tuple = ('a', 'br') ('a', 'b')

print("\nTuple with the use of String: ") Tuple using List:


# Accessing element from last
First element of tuple
print(Tuple 1
# negative indexing
# Creating a Tuple with Last element of tuple
print("\nLast element of tuple") 6
# the use of list
print(Tuple[-1]) Third last element of tuple
list1 = [1, 2, 4, 5, 6] 4

print("\nTuple using List: ")


print("\nThird last element of tuple")
Tuple = tuple(list1)
print(Tuple[-3])
SET
1. Python Set is an unordered collection of data that is mutable and does not
allow any duplicate element.
2. Sets are basically used to include membership testing and eliminating
duplicate entries.
3. The data structure used in this is Hashing, a popular technique to perform
insertion, deletion, and traversal in O(1) on average.
4. If Multiple values are present at the same index position, then the value is
appended to that index position, to form a Linked List.
SET EXAMPLE
# Creating a Set with
print("\nElements of set: ")
# a mixed type of values
for i in Set: Output
# (Having numbers and Set with the use of Mixed Values
{1, 2, 'A', 4, 6, 'B'}
strings) print(i, end =" ")
Elements of set:
Set = set([1, 2, 'A', 4, B', 6, 'A']) print() 1 2 A 4 6 B
True
print("\nSet with the use of
Mixed Values")
# Checking the element
print(Set)
# using in keyword

print("A" in Set)
# Accessing element using

# for loop
Function
def add(a, b):

return a + b

print(add(2, 3))
Lambda function
Anonymous, single-expression functions

square = lambda x: x ** 2

print(square(5))
Recursive FunctioN

A function that calls itself to solve a smaller subproblem.

def factorial(n):
if n == 1:
return 1
return n * factorial(n-1)
print(factorial(5))
def fib(n):

if n <= 1:

return n

return fib(n-1) + fib(n-2)

print(fib(6))
Function
Function Scope & Lifetime

● Definition: Scope determines the visibility of a variable, and lifetime is how long it exists in
memory.

global_var = 10

def func():

local_var = 5

print(local_var)

You might also like