[go: up one dir, main page]

0% found this document useful (0 votes)
10 views2 pages

code2pdf_679486b750c48

The document provides an introduction to Python programming, covering basic concepts such as variables, lists, and control structures. It includes examples of string manipulation, arithmetic operations, and list management through a grocery list manager. The document also demonstrates various list operations like adding, removing, and accessing elements.

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)
10 views2 pages

code2pdf_679486b750c48

The document provides an introduction to Python programming, covering basic concepts such as variables, lists, and control structures. It includes examples of string manipulation, arithmetic operations, and list management through a grocery list manager. The document also demonstrates various list operations like adding, removing, and accessing elements.

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/ 2

# -*- coding: utf-8 -*-

"""exercise 1 and 2

Automatically generated by Colab.

Original file is located at


https://colab.research.google.com/drive/18oCJGy1ot-cjzsvMkDOr1nT68gczy9gH

exercise 1 INTRODUCTION TO PYTHON PROGRAMING


"""

x = "hello, world"
print(x)

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

print(f"My name is {name}", f"and i am {age} 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

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

#exercise 3 LISTS

#create lists ("[]")


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

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

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

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

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

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

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 ")

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')

#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
list=[1,2,3,4]
list.extend([5,6,7])
print(list)

#remove()
list = ["apple","banana","cherry"]
list.remove("banana")
print(list)

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


list = ["apple","banana","cherry"]
list.pop() #removes the last element
print(list)

#del() mostly same


list = ["apple","banana","cherry"]
del list[1]
print(list)

# for loop in between the program


fruits = ["apple","banana","cherry"]
#whule loop inout given by user

You might also like