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

Day 2 Python Data Structure and Functions

Uploaded by

Deep gaichor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Day 2 Python Data Structure and Functions

Uploaded by

Deep gaichor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Day 2: Python Data Structures and Functions

Lists, Tuples, Dictionaries, Sets: Python provides several built-in data structures for
organizing and storing data:

 Lists: Ordered collection of items. Mutable, meaning they can be modified after
creation.
 Tuples: Similar to lists but immutable, meaning they cannot be modified after
creation.
 Dictionaries: Collection of key-value pairs. Keys are unique and immutable, values
can be of any data type.
 Sets: Unordered collection of unique items. Useful for mathematical operations like
union, intersection, etc.

List Comprehensions: List comprehensions provide a concise way to create lists. They
consist of an expression followed by a for clause, then zero or more for or if clauses.

# Example of list comprehension


numbers = [1, 2, 3, 4, 5]
squared_numbers = [num ** 2 for num in numbers]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]

Functions: Functions are blocks of reusable code that perform a specific task. They improve
code modularity and reusability.

# Example of defining and calling a function


def greet(name):
return "Hello, " + name + "!"

print(greet("Alice")) # Output: Hello, Alice!

Lambda Functions: Lambda functions, also known as anonymous functions, are small,
single-expression functions without a name.

# Example of a lambda function


add = lambda x, y: x + y
print(add(3, 4)) # Output: 7

Basic Input/Output Operations: Python provides built-in functions for taking input from
the user and displaying output.

# Example of input/output operations


name = input("Enter your name: ")
print("Hello, " + name + "!")

Output:

Enter your name: Bob


Hello, Bob!
These are some of the foundational concepts of Python programming that you'll use
extensively in your journey as a Python programmer.
Day 2: Python Data Structures and Functions
Lists, Tuples, Dictionaries, Sets
Lists:

 Ordered, mutable, allows duplicate elements


 Created using square brackets []
 Example:

fruits = ["apple", "banana", "cherry"]

 Common methods:
o append(): Adds an element at the end
o remove(): Removes the first occurrence of an element
o pop(): Removes an element at a given index

Tuples:

 Ordered, immutable, allows duplicate elements


 Created using parentheses ()
 Example:

fruits = ("apple", "banana", "cherry")

 Accessed via index: fruits[0]

Dictionaries:

 Key-value pairs, unordered, mutable


 Created using curly braces {} with colons :
 Example:

student = {"name": "Alice", "age": 25, "courses": ["Math",


"Science"]}

 Common methods:
o keys(): Returns a view object of all keys
o values(): Returns a view object of all values
o items(): Returns a view object of all key-value pairs

Sets:

 Unordered, mutable, no duplicate elements


 Created using curly braces {} or the set() function
 Example:

fruits = {"apple", "banana", "cherry"}


 Common methods:
o add(): Adds an element
o remove(): Removes an element
o union(): Returns a set containing all elements from both sets

List Comprehensions
 Concise way to create lists
 Syntax: [expression for item in iterable if condition]
 Example:

squares = [x**2 for x in range(10)]


even_squares = [x**2 for x in range(10) if x % 2 == 0]

Functions: Defining and Calling Functions, Lambda


Functions
Defining and Calling Functions:

 Use the def keyword


 Example:

def greet(name):
return f"Hello, {name}!"

print(greet("Alice"))

Lambda Functions:

 Anonymous functions defined with the lambda keyword


 Syntax: lambda arguments: expression
 Example:

add = lambda x, y: x + y
print(add(2, 3))

Basic Input/Output Operations


Input:

 Using input() to get user input


 Example:

name = input("Enter your name: ")


print(f"Hello, {name}!")

This concludes the note for Day 2: Python Data Structures and Functions.

You might also like