Python Fundamentals

What You'll Find in This Post


Joins are used to join a list with a particular character or a sequence of character

letters = [‘a’, ‘b’, ‘c’, ‘d’]
print ” “.join(letters) # Prints a b c d
print “+++”.join(letters) #Prints a+++b+++c+++d


Try out the example given below and see the result you are getting. Firstly go through the code and try to predict the answer that will appear in the console and finally run the code to verify whether the value in the console matches with the answer you interpreted while going through the code.


for i in range(5):


def print_boat(boat):

   for item in boat:

       print ” “.join(item)


You now have a fair bit of knowledge on how to take the data from the user and how to make a list. Using this, try to make a program which takes number of rows and number of columns from the user and make a matrix of size row*col.


row = int(raw_input(“Guess Row:”))

col = int(raw_input(“Guess Col:”))

//Now define the logic to make a matrix of row”*col size.


Suppose you go to a shop and you want to buy only fruits if you have money between 10 dollars and 20 dollars and you want to buy fruits and vegetables too if you have more than 20 dollars. How will you go about coding such problem. You then will require if, else and elif conditions to write a code for it. In if or else condition, You can have more than one condition also linked with each other through ‘and’ and ‘or’.

Example :

if money > 10 and money <20 :


elif money >20 and money<40:




NOTE: We always use a colon(‘:’) to indicate an end of if or else clause while writing our code.


Recommended Post   Let’s make a Calculator

Question : Write a code to take number of people, cars and trucks from the user and if cars are greater than truck , print “We should take the cars”, if cars are greater than people then print “We should not take the cars” and in all other scenario print “We can’t decide.”

Solution :

people = int(input(“Enter number of people”))
cars = int(input(“Enter number of cars”))
trucks = int(input(“Enter number of trucks”))

if cars > people:
   print “We should take the cars.”
elif cars < people:
   print “We should not take the cars.”
   print “We can’t decide.”

Python tuples

A tuple is another sequence data structure that is used to store data. A tuple consists of a number of values separated by commas just like lists but they are enclosed with ‘(’ and ‘)’. The major difference between tuple and lists is that tuples cannot be updated and can only be accessed whereas lists can be accessed as well as updated.

Tuples can be used to store username which can not be altered. Tuples can be thought of as permanent list which is used only for accessing and storing values.

tuple = ( ‘hello’, ‘python!’ ,’You’, ‘rock!!!!’, 7  )
list = [‘hello’, ‘python!’ ,’You’, ‘rock!!!!’, 7]

print tuple           # Prints complete tuple
print tuple[3]        # Prints fourth element of the tuple
print tuple[2:3]      # Prints elements starting from 3nd till 4rd element
print tuple[2:]       # Prints elements starting from 3rd element
tuple[1] = ‘java’     # This will throw an error as tuple can’t be updated

list[1] = ‘java’      # Update the 2nd element of list to java

Content Protection by