0% found this document useful (0 votes)
0 views3 pages

Module 1 Python

The document provides an overview of tuples and dictionaries in Python, detailing their syntax, characteristics, and examples. It includes a comparison table highlighting their differences, as well as Python programs demonstrating list operations such as sum, average, and square calculations. Additionally, it explains common list functions like append, extend, insert, and index with examples.

Uploaded by

Leroy Golconda
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)
0 views3 pages

Module 1 Python

The document provides an overview of tuples and dictionaries in Python, detailing their syntax, characteristics, and examples. It includes a comparison table highlighting their differences, as well as Python programs demonstrating list operations such as sum, average, and square calculations. Additionally, it explains common list functions like append, extend, insert, and index with examples.

Uploaded by

Leroy Golconda
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/ 3

MODULE – 1

1.

Tuples and Dictionaries in Python


Tuples
Syntax:
A tuple is created by placing elements inside parentheses () separated by commas.
my_tuple = (10, 20, 30)
Characteristics:
 Ordered: Elements have a fixed position.
 Immutable: Once created, elements cannot be changed.
 Allows duplicates: Repeated values are allowed.
 Can store different data types: integers, strings, etc.
Example:
person = ("Alice", 25, "Doctor")
print(person[0]) # Output: Alice
# person[1] = 26 # This will give an error (immutable)

Dictionaries
Syntax:
A dictionary is created using curly braces {} with key-value pairs.
my_dict = {"name": "Bob", "age": 30}
Characteristics:
 Unordered before Python 3.7; now maintains insertion order.
 Mutable: Values can be added, updated, or removed.
 Keys are unique and must be immutable (e.g., strings, numbers).
 Useful for fast lookups using keys.
Example:
student = {"name": "John", "grade": "A"}
print(student["grade"]) # Output: A
student["grade"] = "A+" # Updating value
print(student) # Output: {'name': 'John', 'grade': 'A+'}

Comparison Table
Feature Tuple Dictionary

Syntax () {}

Type Ordered, Immutable Ordered (from Python 3.7+), Mutable

Access by Index Key

Allows Duplicates Yes No (keys must be unique)

Use Case Fixed collections Key-value storage

2. Write a Python program that takes a list of numbers as input and


prints the sum and average of the numbers.
Code:
numbers = input("Enter numbers separated by commas: ")
number_list = [int(num.strip()) for num in numbers.split(",")]
total = sum(number_list)
average = total / len(number_list)
print("Sum:", total)
print("Average:", average)

Sample Output:
Enter numbers separated by commas: 10, 20, 30, 40
Sum: 100
Average: 25.0
3. Explain the following list functions with examples and
explanation:
a) append():
- Adds a single item to the end of the list.
Example:
fruits = ["apple", "banana"]
fruits.append("orange")
print(fruits) # ['apple', 'banana', 'orange']
b) extend():
- Adds multiple elements from another list to the end.
Example:
fruits = ["apple", "banana"]
fruits.extend(["orange", "grape"])
print(fruits) # ['apple', 'banana', 'orange', 'grape']

c) insert():
- Inserts an element at a specific index.
Example:
fruits = ["apple", "banana"]
fruits.insert(1, "orange")
print(fruits) # ['apple', 'orange', 'banana']

d) index():
- Returns the index of the first occurrence of a value.
Example:
fruits = ["apple", "banana", "orange"]
print(fruits.index("orange")) # 2
4. Write a Python program to make a new list that will store squares
of elements from the existing list [10, 8, 6, 4, 2].
Code:
numbers = [10, 8, 6, 4, 2]
squares = [x**2 for x in numbers]
print("Original list:", numbers)
print("Squares list:", squares)

Output:
Original list: [10, 8, 6, 4, 2]
Squares list: [100, 64, 36, 16, 4]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy