You are on page 1of 3

Exp 1

Code:
print("STRING")
# Creating a string
string_var = "Hello, World!"
# Accessing characters in a string
print(string_var[0])
# Slicing a string
print(string_var[7:])
# String concatenation
new_string = string_var + " How are you?"
print(new_string)

print("\n")
print("LIST")
# Creating a list
my_list = [1, 2, 3, "apple", "banana"]
# Accessing elements in a list
print(my_list[0])
# Slicing a list
#print(my_list[2:4]) # Output: [3, 'apple']
# Modifying a list
my_list.append("orange")
print(my_list) # Output: [1, 2, 3, 'apple', 'banana', 'orange']

print("\n")
print("DICTIONARY")
# Creating a dictionary
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# Accessing values in a dictionary
print(my_dict['name'])
# Modifying values in a dictionary
my_dict['age'] = 26
print(my_dict) # Output: {'name': 'John', 'age': 26, 'city': 'New York'}

print("\n")
print("SET")
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Adding and removing elements from a set
my_set.add(6)
my_set.remove(3)
print(my_set) # Output: {1, 2, 4, 5, 6}

print("\n")
print("TOUPLE")
# Creating a tuple
my_tuple = (1, 2, 3, "apple", "banana")
# Accessing elements in a tuple
print(my_tuple[3]) # Output: apple
print("\n")
print("IF ELSE STATEMENTS")
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")

print("FOR LOOP")
# Iterating over a list
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)

print("WHILE LOOP")
# Simple while loop
count = 0
while count < 5:
print(count)
count += 1

print("BREAK AND CONTINUE")


# Using break and continue in a loop
for i in range(10):
if i == 5:
break # exit the loop when i is 5
if i % 2 == 0:
continue # skip the even numbers
print(i)

Output:

You might also like