Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Data Analytics Using Python/1 Python_Data_Structures_Basics.ipynb
3074 views
Kernel: Python 3 (ipykernel)

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

a=["abc",12.4,-2,"b"] a len(a) ## access the member a[1] a[-1]
# Creating a list fruits = ["apple", "banana", "cherry"] fruits
# Adding an item fruits.append("orange") print("After append:", fruits)
fruits.insert(2,"grapes")
fruits
# Removing an item fruits.remove("banana") print("After removal:", fruits)
del fruits[2]

Common List Methods

  • append(item) – Add item to end

  • insert(index, item) – Insert at a position

  • remove(item) – Remove first occurrence

  • pop(index) – Remove item at index

  • sort() – Sort list

  • reverse() – 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.

# Working with strings text = "Hello, Python!" print(text) print("Uppercase:", text.upper()) print("Lowercase:", text.lower()) print("First 5 chars:", text[:5])
del text[4]
len(text)
text.append("folks")

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 uppercase

  • lower() – Convert to lowercase

  • strip() – Remove surrounding whitespace

  • replace(old, new) – Replace substrings

  • find(sub) – Return index of substring

3. Tuples

Tuples are immutable sequences, often used to store related items.

d=() d=("a",2,3,) #del d[0] d d[0]
# Creating a tuple person = ("Alice", 30, "Engineer") print(person) print("Name:", person[0])

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()

d={} d["A"]=12 d["B"]=14 d d["A"] del d["A"] d
# Creating and accessing a dictionary student = {"name": "Bob", "age": 21, "course": "Data Science"} print(student) print("Name:", student["name"]) # Adding a new key-value pair student["grade"] = "A" print("Updated student:", student)
d["B"]=19 d["c"] = (6,7) d.keys() d.values() d.items() d.pop("c")

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 key

  • keys() – Return all keys

  • values() – Return all values

  • items() – Return key-value pairs

  • update(dict) – Update with another dict

  • pop(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:

n="piyush" d=[1,2,3]
for i in range(1,6): print(i)
for i in range (0,12,2): print(i)
for i in range (8,2,-1): print(i)
for i in (n): int(input())
colors = ["red", "green", "blue"] for color in colors: print(f"Color: {color}")
### Input number of elements n = int(input("Enter number of elements in the list: ")) # Initialize empty list my_list = [] # Loop to input elements for i in range(0,n): element = input(f"Enter element {i+1}: ") my_list.append(element) print("List:", my_list)

While Loop Example

num = 0 while num <= 5: print("Number:", num) num = num+1
Number: 0 Number: 1 Number: 2 Number: 3 Number: 4 Number: 5

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

d={} d[1]="A" d[2]="B" d
{1: 'A', 2: 'B'}
# Input number of key-value pairs n = int(input("Enter number of key-value pairs in the dictionary: ")) # Initialize empty dictionary emp = {} # Loop to input keys and values for i in range(n): empID = input(f"Enter ID {i+1}: ") Name = input(f"Enter value for '{empID}': ") emp[empID] = Name print("Dictionary:", emp)
Enter number of key-value pairs in the dictionary: 3 Enter ID 1: 1 Enter value for '1': Abha Enter ID 2: 2 Enter value for '2': Ashi Enter ID 3: 4 Enter value for '4': kia Dictionary: {'1': 'Abha', '2': 'Ashi', '4': 'kia'}