Printing "Hello World" on the output console:
print('Hello world')
Addition:
100+20
Subtraction:
200-40
Multiplication:
45*67
Complex mathematical operations:
4*6*(28+5)
Division:
200/2
Floor division:
If we do not want to get a decimal value we could use // instead of /.
200//2
Divide by zero:
345/0
This will give a divide by zero error.
Mathematical operations with float:
12.0 * 4
Modulus:
21%4
Modulus with float:
456.34%2
Clearing Python Shell:
On Windows:
import os os.system('cls')
On Mac/Linux:
import os os.system('clear')
Escaping a single quote: i.e a " ' ":
'He\\'s a good guy'
Output:
" He's a good guy "
Using single quotes to escape double quotes:
'This is the "only" way to go'
Escaping multiple double quotes:
"This is the \"only\" way to go"
input('Enter your name')
To check the data type:
type(8) type('8')
String operations
Concat:
"Hello" + " world"
Including space between two strings:
"Hello " + "World"
Concatenating two numbers as a string:
"10" + "10"
Concatenating string & number as a string.
"Hello " + "5"
Concatenating string with a number
"Hello" + 5
Multiplying a string with a number
"Hello " * 4
Multiplying a string with another string
"Hello" * "Hi"
Integer:
print(10) type(10)
Float or floating point numbers:
type(6.2)
A boolean type :
>>> True True >>> False False >>> type(True) <class 'bool'> >>> type(False) <class 'bool'>
Complex numbers:
5+9j
Declaring a variable:
a = 200
Declaring another variable b:
b=20
Value of a variable can be changed at any instance:
a= 100 print(a) a=200 print(a)
String variables:
name = "John" type(name)
Float Variable:
a= 3.2 type(a)
Boolean Variable:
b= True type(b)
a = int(input("Enter First Number")) b = int(input("Enter second Number")) print(a+b)
firstname = input("Enter firstname: ") lastname = input("Enter lastname: ") username = firstname+lastname email = firstname+lastname+"@gmail.com" print("Username is: "+username) print("Email is: "+email)
saved_password = "123admin123" user_password = input("Enter password") print(saved_password==user_password)
principal = int(input("Enter amount lent")) years = float(input("Enter lending period in years")) rate = float(input("Enter rate of interest")) interest = principal * years * rate / 100 print("Simple interest calculated is"+ str(interest))
principal = int(input("Enter amount lent")) years = float(input("Enter lending period in years")) rate = float(input("Enter rate of interest")) interest = principal * years * rate / 100 print(f"Simple interest on the principal amount $ {principal} for a period of {years} years at the rate of {rate} is $ {interest}")
weight = float(input('Enter weight in kgs')) height = float(input('Enter height in meters')) bmi = weight/(height*height) print('BMI is: '+str(bmi))