0% found this document useful (0 votes)
6 views5 pages

Sample Progs

Uploaded by

rashmikudubi12
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)
6 views5 pages

Sample Progs

Uploaded by

rashmikudubi12
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/ 5

1

Python Sample Programs

Lists

 Ordered collections of elements.


 Mutable: Elements can be changed after creation.
 Can contain elements of different data types.
 Indexed: Elements can be accessed by their position.

Example:

Python
my_list = [1, 2, 3, "hello", True]

Tuples

 Ordered collections of elements.


 Immutable: Elements cannot be changed after creation.
 Can contain elements of different data types.
 Indexed: Elements can be accessed by their position.

Example:

Python
my_tuple = (1, 2, 3, "hello", True)

Sets

 Unordered collections of unique elements.


 Mutable: Elements can be added or removed.
 Cannot contain duplicate elements.
 Do not preserve element order.

Example:

Python
my_set = {1, 2, 3, "hello"}

Dictionaries

 Unordered collections of key-value pairs.


 Mutable: Key-value pairs can be changed.
 Keys must be unique.
 Values can be of any data type.

Example:

Python
my_dict = {"name": "Alice", "age": 30, "city": "New York"}

Choosing the Right Data Structure:

 Lists: Use when you need an ordered collection of elements that can be modified.
 Tuples: Use when you need an ordered collection of elements that should not be modified.
2

 Sets: Use when you need a collection of unique elements without preserving order.
 Dictionaries: Use when you need to store key-value pairs.

Example of Using Different Data Structures:

Python
# List of fruits
fruits = ["apple", "banana", "orange"]

# Tuple of colors
colors = ("red", "green", "blue")

# Set of numbers
numbers = {1, 2, 3, 4, 5}

# Dictionary of student information


students = {"Alice": 25, "Bob": 30, "Charlie": 28}

# Create a list of fruits


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

# Access elements by index


print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: orange

# Modify elements
fruits[1] = "grape"
print(fruits) # Output: ['apple', 'grape', 'orange']

# Append an element
fruits.append("mango")
print(fruits) # Output: ['apple', 'grape', 'orange', 'mango']

# Insert an element at a specific index


fruits.insert(2, "kiwi")
print(fruits) # Output: ['apple', 'grape', 'kiwi', 'orange', 'mango']

# Remove an element by value


fruits.remove("orange")
print(fruits) # Output: ['apple', 'grape', 'kiwi', 'mango']

# Remove an element by index


del fruits[1]
print(fruits) # Output: ['apple', 'kiwi', 'mango']

# Python3 program to add two numbers

num1 = 15

num2 = 12

# Adding two nos


3

sum = num1 + num2

# printing values

print("Sum of", num1, "and", num2 , "is", sum)

# Python3 program to add two numbers

number1 = input("First number: ")


number2 = input("\nSecond number: ")

# Adding two numbers


# User might also enter float numbers
sum = float(number1) + float(number2)

# Display the sum


# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1,
number2, sum))

#To define a function that take two integers


# and return the sum of those two numbers
def add(a,b):
return a+b

#initializing the variables


num1 = 10
num2 = 5

#function calling and store the result into sum_of_twonumbers


sum_of_twonumbers = add(num1,num2)

#To print the result


print("Sum of {0} and {1} is {2};" .format(num1,
num2, sum_of_twonumbers))

# Define a lambda function to add two numbers


add_numbers = lambda x, y: x + y

# Take input from the user


num1 = 1
num2 = 2

# Call the lambda function to add the two numbers


result = add_numbers(num1, num2)

# Print the result


print("The sum of", num1, "and", num2, "is", result)

# Python program to find the


# minimum of two numbers
4

def minimum(a, b):

if a <= b:
return a
else:
return b

# Driver code
a=2
b=4
print(minimum(a, b))

# Python program to find the


# minimum of two numbers

a=2
b=4

minimum = min(a, b)
print(minimum)

# Python program to print negative Numbers in a List

# list of numbers
list1 = [11, -21, 0, 45, 66, -93]

# iterating each number in list


for num in list1:

# checking condition
if num < 0:
print(num, end=" ")

# Python program to print negative Numbers in a List

# list of numbers
list1 = [-10, 21, -4, -45, -66, 93]
num = 0

# using while loop


while(num < len(list1)):

# checking condition
if list1[num] < 0:
print(list1[num], end=" ")

# increment num
num += 1
5

You might also like