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

Exercise 1 and 2 - Colab

The document provides an introduction to Python programming, covering basic concepts such as variables, data types, and lists. It includes code examples demonstrating string manipulation, arithmetic operations, and list management, including adding and removing items from a grocery list. The content is structured as exercises to practice Python programming skills.

Uploaded by

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

Exercise 1 and 2 - Colab

The document provides an introduction to Python programming, covering basic concepts such as variables, data types, and lists. It includes code examples demonstrating string manipulation, arithmetic operations, and list management, including adding and removing items from a grocery list. The content is structured as exercises to practice Python programming skills.

Uploaded by

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

1/25/25, 9:51 AM exercise 1 and 2 - Colab

exercise 1 INTRODUCTION TO PYTHON PROGRAMING

x = "hello, world"
print(x)

hello, world

name = "david"
age = 17
print("My name is", name, "and i am ", age, "years old")

My name is david and i am 17 years old

print(f"My name is {name}", f"and i am {age} years old")

My name is david and i am 17 years old

exercise 2 VARIABLES IN PYTHON

one = 1
two = 2
three = one + two
print(three)

one = 1
two = 2
hello = "hello"

print(one + two , hello) #cannot out (one + two + hello) output>>> error because one and two are int and hello is a string

3 hello

mystring = hello
myfloat = 10.0
myint = 20

if mystring =="hello": # check wther the string is treu or not (hello)


print("String: %s" % mystring) # instead of typing of "mysting" we can put %s
if isinstance(myfloat, float) and myfloat == 10.0:
print("float: %f" %myfloat) # same as %s here we will use %f
if isinstance(myint, int) and myint == 20:
print("integer: %d" % myint) # and here %d

String: hello
float: 10.000000
integer: 20

#exercise 3 LISTS

#create lists ("[]")


thislist=['apple','banana','cherry']
print(thislist)

['apple', 'banana', 'cherry']

thislist=['apple','banana','cherry']
print(thislist)
print(len(thislist)) # len function is used to determine how many items are there in the list

['apple', 'banana', 'cherry']


3

thislist=['apple','banana','cherry']
print(thislist)
print(type(thislist)) # data type

https://colab.research.google.com/drive/18oCJGy1ot-cjzsvMkDOr1nT68gczy9gH#scrollTo=JxJIZmdv8_uO 1/3
1/25/25, 9:51 AM exercise 1 and 2 - Colab

['apple', 'banana', 'cherry']


<class 'list'>

thislist= list(("apple:","banana","cherry"))
print(thislist) # list() -> list

['apple', 'banana', 'cherry']

thislist=['apple','banana','cherry']
thislist[:2]=['guava','watermelon'] # changin
print(thislist)

['guava', 'watermelon', 'cherry']

thislist=['apple','banana','cherry']
thislist[-1]

'cherry'

grocery_list=[]

def display_menu():
print("\nGrocery List Mnanager")
print("1. Add item")
print("2. Remove item")
print("3. View list")
print("4. Exit")

def add_item():
item=input("Enter the item to add")
grocery_list.append(item)
print(f"{item} has been added to the list.")

def remove_item():
item = input("Enter item to remove: ")
if item in grocery_list:
grocery_list.remove(item)
print(f"{item} has been removed from the list.")
else:
print(f"{item} is not in the list.")

def view_list():
if grocery_list:
print("\nYour grocery list:")
for idx, item in enumerate(grocery_list, start=1):
print(f"{idx}. {item}")
else:
print("Your grocery list is empty.")

while True:
display_menu()
choice = input("Choose an option (1-4): ")

if choice == "1":
add_item()
elif choice == "2":
remove_item()
elif choice == "3":
view_list()
elif choice == "4":
print("Exiting the Grocery List Manager. Goodbye!")
break
else:
print("Invalid choice. Please try again ")

Show hidden output

fruits=["apple","banana","cherry"]
if "apple" in fruits: # in func helps to check if the data is present
print("yes, apple is a fruit")
else:
print('false')

yes, apple is a fruit

https://colab.research.google.com/drive/18oCJGy1ot-cjzsvMkDOr1nT68gczy9gH#scrollTo=JxJIZmdv8_uO 2/3
1/25/25, 9:51 AM exercise 1 and 2 - Colab
#replacing elements in a list
fruits=["apple","banana","cherry"]
fruits[1]=["hahaha"]
print(fruits)

#append()
thislist=['apple','banana','cherry']
thislist.append("orange")
print(thislist)

#insert ()
thislist=['apple','banana','cherry']
thislist.insert(2,"watermelon")
print(thislist)

#extend list

#remove()
#pop() if we dont specify by default itll remove the last one
#del() mostly same

Start coding or generate with AI.

https://colab.research.google.com/drive/18oCJGy1ot-cjzsvMkDOr1nT68gczy9gH#scrollTo=JxJIZmdv8_uO 3/3

You might also like