Python Training and Exercises

Getting version of Python

>>> import sys

>>> print (sys.version)
3.5.2 (default, Sep 14 2017, 22:51:06) 

				

Different ways to assign value to variables

>>>a = 10
>>>a,b=10,20
>>>a=b=c=10
>>>a,b=10, "hello"

Note:  a= 10 and a= '10' are not same. 

In the first command “a” is of type integer and in the second “a” is a string

You can use single quotes or double quotes or triple quotes to create strings

There is no difference in the functionality of single quote and double quote in python

>>> a = 'hello'
>>> b = 'hello world'
>>> c = "hello"
>>> d = "hello world"

You can use three single quotes (''') or three double quotes to create a string with multiple lines

 >>> a ='''Interface eth0 is up
	... IP is 192.168.1.1
	... Mac is 00:11:22:33:44:55 '''
>>> 

>>> b = """Interface eth0 is up
... IP is 192.168.1.1
... Mac is 00:11:22:33:44:55"""
>>> 

				

Getting input from keyboard/stdin

>>> a = input("enter something: ")

enter something: hello world

>>> print (a)
hello world

>>> b = int(input("enter a number: "))

enter a number: 100
>>> print (b)
100


				

Different ways of using print function

>>> a = 10
>>> b="hello"

>>> print (a)
10

>>> print (a,b)
10 hello

>>> print ("a is",a)
a is 10

>>> print ("a is %d" %a)
a is 10
>>> 

>>> print ("a is %d b is %s" %(a,b))
a is 10 b is hello

>>> print ("b is  " + b)
b is hello

Note: Concatenating with “+” will only work for strings

>>> a = 10
>>> print ("a is " +a )
TypeError: Can't convert 'int' object to str implicitly
>>> 

>>> print ("a is " +str (a))
a is 10


>>> print ("Interface "EthernetO" is up")
    print ("Interface "EthernetO" is up")
                               ^
SyntaxError: invalid syntax

>>> print ("Interface \"EthernetO\" is up")
Interface "EthernetO" is up

>>> print ("Interface 'EthernetO' is up")
Interface 'EthernetO' is up

>>> print ('Interface "EthernetO" is up')
Interface "EthernetO" is up

>>> print ('Interface 'EthernetO' is up')
  File "<stdin>", line 1
    print ('Interface 'EthernetO' is up')
                               ^
SyntaxError: invalid syntax

				

Checking the type of a variable using “type” function

>>> a,b=10,"hello"

>>> type(a)
<class 'int'>

>>> type(b)
<class 'str'>

>>> c=10.2
>>> type(c)
<class 'float'>

>>> d = [10,20,30]
>>> type(d)
<class 'list'>

>>> e=(100,200,300)

>>> type(e)
<class 'tuple'>
>>> 

>>> type(print)
<class 'builtin_function_or_method'>


				

Using Python membership operators

>>> a= "hello world"

>>> 'e' in a
True

>>> 'ello' in a
True

>>> 'xy' in a
False

>>> 'e' not in a
False
>>> 'xy' not in a
True
>>> 


				

If , elif, else examples

a,b,c=10,20,30

if a == 10:
   	 print ("hello")

 
if a==10 and b <25:
  	 print ("hello")
 
if a==10 or b <25:
  	 print ("hello")

if a>10:
	print ("hello")
else:
	print ("hi")
 
 
if a <10:
	  print ("hello")
elif a>10:
	  print ("hello world")
else:
	  print ("hi")
	 

if a==10 and b<=30:
	print ("hello")
	if c < 50:
		print ("hello world")
	else:
		print ("hi")
 

				

Python Exercises

1. Write a python program to swap two numbers using a third variable

2. Write a python program to swap two numbers without using third variable

3. Write a python program to read two numbers and find the sum of their cubes

4. Write a python program to read three numbers and if any two variables are equal , print that number

5. Write a python program to read three numbers and find the smallest among them

6. Write a python program to read three numbers and print them in ascending order (with out using sort function)

7. Write a python program to read radius of a circle and print the area

8. Write a python program to read four numbers (representing the four octets of an IP) and check whether they all are in the range between 0 to 255

9. In the above program, if all numbers are in the valid range, print which class of IP it is.

10. Write a python program to read a number ,if it is an even number , print the square of that number and if it is odd number print cube of that number.

Next chapter: Python training : While loops, For loops and Range function



CONTACT

Contact us and we'll get back to you within 24 hours.

#11, 8th cross, Indiranagar 1st stage, Bangalore, India

Queries?
You can connect with us on Whatsapp 8884 884 844