Path: blob/master/Data Analytics Using Python/1 Python_Data_Structures_Basics.ipynb
3074 views
Python Basics: Lists, Strings, Tuples, and Dictionaries
This notebook provides a simple explanation and quick practice for Python's core data structures.
1. Lists
Lists are mutable collections used to store multiple items in a single variable. []
it can have multiple datatypes
Common List Methods
append(item)
– Add item to endinsert(index, item)
– Insert at a positionremove(item)
– Remove first occurrencepop(index)
– Remove item at indexsort()
– Sort listreverse()
– Reverse list
Quick Practice:
Create a list of your 3 favorite movies.
Add one more movie.
Remove the second movie.
Print the final list.
2. Strings
Strings are sequences of characters enclosed in quotes. They are immutable.
Quick Practice:
Create a string with your name.
Print it in uppercase and lowercase.
Slice and print the first 4 characters.
Common String Methods
upper()
– Convert to uppercaselower()
– Convert to lowercasestrip()
– Remove surrounding whitespacereplace(old, new)
– Replace substringsfind(sub)
– Return index of substring
3. Tuples
Tuples are immutable sequences, often used to store related items.
Quick Practice:
Create a tuple with your name, age, and city.
Print each element using indexing.
4. Dictionaries
Dictionaries are collections of key-value pairs.
Tuple Characteristics and Functions
Immutable: Cannot be changed after creation
Can contain mixed data types
Supports indexing and slicing
Functions:
len()
,count()
,index()
Quick Practice:
Create a dictionary for a car with keys: brand, model, year.
Print the model.
Add a new key: color and assign a value.
Common Dictionary Methods
get(key)
– Return value for keykeys()
– Return all keysvalues()
– Return all valuesitems()
– Return key-value pairsupdate(dict)
– Update with another dictpop(key)
– Remove key and return value
5. Loops in Python
Python supports two main types of loops: for
and while
. They are used to execute a block of code repeatedly.
For Loop Example
Loop through a list and print each item:
While Loop Example
Questions:
Create a user input list to dipaly even number between givem range
Create a user input dictionary for Name as key and Age as Value