Pixeltests 0 Comments

Python Made Easy for AI – Day 4.3


Lists

Excercise – 1:

List your Family Members in a List in this sequence: Mom, Dad, Elder One

family = [“Indira”, “Madhu”, “Somu”];

if len(family) > 0:
 print (“Sweet mom is ” + family[0]) #Accessing by index
 print (“Caring Dad is  ” + family[1])
 print (“Elder one is ” + family[2])

print (family)


Excercise – 2:


Add to the list siblings of elder, update name of a member

family.append(“Sachi”)
print (len(family))
print (family)

#Updating
family[3] = “Savyasachi”
print (family)

#Slicing
parents = family[0:2] #Excluding last index
print (parents)
siblings = family[2:]
print (siblings)

Excercise – 3:

Make a set of Numbers and perform arithemetic between them

numbers = [5, 6, 7, 8]
print (“Adding the numbers at indices 0 and 2”)
print (numbers[0] + numbers[2])
print (“Adding the numbers at indices 1 and 3”)
print (numbers[1] *  numbers[3])

Loops

Excercise – 1:

Use While to Print count 5 times

count = 0

if count < 5:
 print (“Hello, I am an if statement and count is”, count)

while count < 5: #Condition
 print (“Hello, I am a while and count is”, count)
 count += 1


Excercise – 2:


Print Square of a numbers from 1 to 10 using while

num = 1
while num <= 10:  # Fill in the condition  
   # Print num squared  
   print (num * num)

   # Increment num (make sure to do this!)  
   num += 1

Excercise – 3:

Print Square of a numbers from 1 to 5 using for

numbers  = [1, 2, 3, 4, 5]  #Using Lists
print (numbers)

print(“Squared Numbers are:”)

# Add your loop below!
for num in numbers:
 print num * num

print(“Squared Numbers are:”)

# Add your loop below!
for num in range(1,6): #Using Range
 print (num * num)

Leave a Comment