Programming in Python
1. Introduction to Python
Type the below code in the IDLE and check the output.
print(2)
Use the following codes to perform basic arithmetic operations.
print(2+3)
print(2*3)
print(2**3)
print(45/5)
2**3 means 2 to the power of 3 which gives a result of 8 whereas 2*3 just multiplies 2 and 3 which gives a result of 6.
print(2*3)
print(2**3)
print(45/5)
Lets print a sentence
print("Hi, Welcome all")
Now, lets store the number in an object
a = 10
Now, print the value of a
print(a)
Comment line in python
#print(a)
Note that the commands following with '#' wont't be executed but will be displayed
Now, lets store the another number in an object
b = 30
Multiply a and b and print the result
print(a*b)
Let's print the string 'Hello' 10 times
print("Hello" * 10)
print("Hello" * a)
print("Hello" * a)
Both the statements will print 'Hello' 10 times because we have stored the value 10 in 'a'
Now, lets add two strings
print("Hi, " + "how are you?")
Try to add different datatypes
print("Hi" + 10)
This will throw a TypeError: can only concatenate str (not "int") to str
Now, we should understand what are the different datatypes available in python which will be covered in the next section