Opening Hours :7AM to 9PM
a = 5 print('The value of a is', a) # Output: The value of a is 5
print(1,2,3,4) # Output: 1 2 3 4 print(1,2,3,4,sep='*') # Output: 1*2*3*4 print(1,2,3,4,sep='#',end='&') # Output: 1#2#3#4&
>>> x = 5; y = 10 >>> print('The value of x is {} and y is {}'.format(x,y)) The value of x is 5 and y is 10
print('I love {0} and {1}'.format('bread','butter')) # Output: I love bread and butter print('I love {1} and {0}'.format('bread','butter')) # Output: I love butter and bread We can even use keyword arguments to format the string. >>> print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John')) Hello John, Goodmorning We can even format strings like the old sprintf() style used in C programming language. We use the % operator to accomplish this. >>> x = 12.3456789 >>> print('The value of x is %3.2f' %x) The value of x is 12.35 >>> print('The value of x is %3.4f' %x) The value of x is 12.3457
>>> num = input('Enter a number: ') Enter a number: 10 >>> num '10' Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions. >>> int('10') 10 >>> float('10') 10.0 This same operation can be performed using the eval() function. But it takes it further. It can evaluate even expressions, provided the input is a string >>> int('2+3') Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive input>", line 1, in <module> ValueError: invalid literal for int() with base 10: '2+3' >>> eval('2+3') 5
import math print(math.pi) Now all the definitions inside math module are available in our scope. We can also import some specific attributes and functions only, using the from keyword. For example: >>> from math import pi >>> pi 3.141592653589793 While importing a module, Python looks at several places defined in sys.path. It is a list of directory locations. >>> import sys >>> sys.path ['', 'C:\\Python33\\Lib\\idlelib', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages'] We can add our own location to this list as well.