Python Practice | Sole Concept Lab

Python Practice Building up basics

Introduction

First and foremost a big shoutout to the datacamp community to enable all of us to use the feature used in this post. You will see what I mean. This is a very brief post in which you will be performing exercises from the basic python and conditional statements and iterators.

Without delay Let me quickly set up the exercises for you. I know you will rok it!!

 

Code Time

List practice

# This is the list that we saw in the previous post a = [1,2,3,4,5,6,7,'a','b', 'c'] # Find the index of the element c and print out only the element c from the list # index the list below and store it in variable c a = [1,2,3,4,5,6,7,'a','b', 'c'] c = # print the value stored in c #Print the last three elements of the list. First store it in a variable d d= #print d # Find the index of the element c and print out only the element c from the list # index the list below and store it in variable c c = a[9] # print the value stored in c print(c) #Print the last three elements of the list. First store it in a variable d d= a[-4:-1] #print d print(d) test_object("c") test_function("print") test_object("d") test_function("print") success_msg("You Rocked it !")
Make sure to use the right indexing and remember the index starts off at 0. Go through the documentation to find how to print the last three elements in the list

Dictionaries practice

# You've got four tasks to perform # (a) Create a dictionary info whose keys are name and age where the values of name and age are Bob and #24 info = # print the keys of the dictionary # print the value corresponding to the key age # change the value of the age to 10 # You've got four tasks to perform # (a) Create a dictionary info whose keys are name and age where the values of name and age are Bob and #24 info = {'name':'Bob','age':24} # print the keys of the dictionary print(info.keys()) # print the value corresponding to the key age print(info['age']) # change the value of the age to 10 info['age'] =10 test_object("info") test_function("print") test_function("print") test_function("print") test_object("info") success_msg("You have rocked it again")
The indexing and replacing is very similar to lists go back to the previous post if you are stuck

Conditional Statements and Iterators

Turning on to the conditional statements now. Remember I told you to input a number check the return type. That’s exactly what you will be doing now.

# Write a code that takes a user input. Make sure the input is a number if not convert it to a number a = ## Ask for user input and make sure you input a number more particularly an int # Print the type of a # Now convert the input to a number b = #pring b print(b) # Write a code that takes a user input. Make sure the input is a number if not convert it to a number a = input('Enter an input: ') ## Ask for user input and make sure you input a number more particularly an int # Print the type of a print(type(a)) # Now convert the input to a number b = int(a) #pring b print(b) test_object("a") test_function("print") test_object("b") test_function("print") success_msg("You've Rocked !")
See if you understand what the input does. Make sure to enter a number

Ok We have not exactly seen what conditional statements but let’s combine it with iterators now.
What you will do is print the number is even by iterating through a list else you will print the number is odd.

# The list is given below a = [1,2,5,8,5,6,9,10,15] # Iterate through the list and print yes if the number is even else print the number is odd. # The list is given below a = [1,2,5,8,5,6,9,10,15] # Iterate through the list and print yes if the number is even else print the number is odd. for i in a: if i%2==0: print("The number is even") else: print("The number is odd")
See if you understand what the input does. Make sure to enter a number

That’s it for now. Just to point out you could also run these codes locally on your pc. But make sure you rock each problem. Happy learning!

Sharing is caring!

Leave a Comment