Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views6 pages

1

The document provides examples of various Python programming concepts, including list operations (append, extend, comprehension, slicing, and removing duplicates), dictionary manipulations (creating, updating, deleting, and merging), and while loops (basic usage, user input, control flow, and calculations). It demonstrates how to combine lists and dictionaries, as well as filtering and sorting techniques. Each section includes code snippets and expected output for clarity.

Uploaded by

koshiki229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

1

The document provides examples of various Python programming concepts, including list operations (append, extend, comprehension, slicing, and removing duplicates), dictionary manipulations (creating, updating, deleting, and merging), and while loops (basic usage, user input, control flow, and calculations). It demonstrates how to combine lists and dictionaries, as well as filtering and sorting techniques. Each section includes code snippets and expected output for clarity.

Uploaded by

koshiki229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Lists - Append and Extend

# Adding elements using append() and extend()

numbers = [1, 2, 3, 4]

numbers.append(5)

numbers.extend([6, 7])

print(numbers)

# Output: [1, 2, 3, 4, 5, 6, 7]

2. Lists - List Comprehension

# List comprehension to create a list of squares

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

print(squares)

# Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

3. Lists - Nested Lists

# Accessing elements in a nested list

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(matrix[1][2])

# Output: 6

4. Lists - Slicing

# Slicing a list to get sublists

fruits = ['apple', 'banana', 'cherry', 'date']

print(fruits[1:3])

# Output: ['banana', 'cherry']

5. Lists - Remove Duplicates

# Removing duplicates from a list

numbers = [1, 2, 3, 2, 1, 4]

unique_numbers = list(set(numbers))

print(unique_numbers)
# Output: [1, 2, 3, 4]

6. Dictionaries - Key-Value Pairs

# Creating and accessing dictionary elements

student = {'name': 'John', 'age': 25, 'courses': ['Math', 'CS']}

print(student['name'])

print(student.get('phone', 'Not Found'))

# Output: John

# Output: Not Found

7. Dictionaries - Update and Delete

# Updating and deleting elements in a dictionary

student = {'name': 'John', 'age': 25, 'courses': ['Math', 'CS']}

student['phone'] = '555-5555'

del student['age']

print(student)

# Output: {'name': 'John', 'courses': ['Math', 'CS'], 'phone': '555-5555'}

8. Dictionaries - Looping Through a Dictionary

# Looping through keys and values in a dictionary

student = {'name': 'John', 'age': 25, 'courses': ['Math', 'CS']}

for key, value in student.items():

print(key, value)

# Output:

# name John

# age 25

# courses ['Math', 'CS']

9. Dictionaries - Nested Dictionaries

# Nested dictionaries example

university = {
'student1': {'name': 'John', 'age': 25},

'student2': {'name': 'Sara', 'age': 22}

print(university['student1']['name'])

# Output: John

10. Dictionaries - Default Values

# Using defaultdict to handle missing keys

from collections import defaultdict

student = defaultdict(lambda: 'Not Found', {'name': 'John', 'age': 25})

print(student['phone'])

# Output: Not Found

11. While Loop - Basic Example

# Basic while loop to print numbers

i=1

while i <= 5:

print(i)

i += 1

# Output:

#1

#2

#3

#4

#5

12. While Loop - User Input

# While loop with user input

number = 0

while number >= 0:

number = int(input("Enter a number (negative to stop): "))


print(number)

# Output will depend on user input.

13. While Loop - Break and Continue

# Using break and continue in a while loop

i=0

while i < 10:

i += 1

if i % 2 == 0:

continue

if i == 7:

break

print(i)

# Output:

#1

#3

#5

14. While Loop - Factorial Calculation

# Calculate factorial using while loop

num = 5

factorial = 1

while num > 0:

factorial *= num

num -= 1

print(factorial)

# Output: 120

15. Lists and Dictionaries Together

# Combining lists and dictionaries

students = [
{'name': 'John', 'age': 25},

{'name': 'Sara', 'age': 22}

for student in students:

print(student['name'])

# Output:

# John

# Sara

16. Lists - Sorting with Key

# Sorting list of dictionaries using a key

students = [

{'name': 'John', 'age': 25},

{'name': 'Sara', 'age': 22}

students.sort(key=lambda x: x['age'])

print(students)

# Output: [{'name': 'Sara', 'age': 22}, {'name': 'John', 'age': 25}]

17. Dictionaries - Merge Two Dictionaries

# Merging two dictionaries

dict1 = {'a': 1, 'b': 2}

dict2 = {'b': 3, 'c': 4}

merged_dict = {**dict1, **dict2}

print(merged_dict)

# Output: {'a': 1, 'b': 3, 'c': 4}

18. While Loop - Guessing Game

# Simple guessing game using while loop

secret_number = 7

guess = 0
while guess != secret_number:

guess = int(input("Guess the number: "))

if guess < secret_number:

print("Too low!")

elif guess > secret_number:

print("Too high!")

print("Congratulations! You guessed it.")

# Output will depend on user input.

19. While Loop - Sum of Digits

# Sum of digits of a number using while loop

num = 12345

sum_of_digits = 0

while num > 0:

digit = num % 10

sum_of_digits += digit

num //= 10

print(sum_of_digits)

# Output: 15

20. Lists - Filter Even Numbers

# Filtering even numbers from a list

numbers = [1, 2, 3, 4, 5, 6]

even_numbers = [num for num in numbers if num % 2 == 0]

print(even_numbers)

# Output: [2, 4, 6]

You might also like