Notes:
Text Datatype: (String)
- str
How can we check datatype of the variable:
type() function:
- It is used to check the datatype of the variable.
Example 1:
firstName = "krunal"
print(type(firstName))
Numeric Datatype:
- You can store number into variable.
int - age - 25, 30, 35, 40
Example 2:
age=45
print(type(age))
float - price - 99.99, 123.45
Example 3:
price=29.99
print(type(price))
Boolean Datatype:
- You can store value in form of either True or False.
Example 4:
lightStatus = False
print(type(lightStatus))
Sequence Type:
List
Tuple
Range
Mapping Type:(pair)
dict
- Key and Value
Input() function:
- It is used to take input from the user in str datatype only.
Example 5:
greeting = input("Enter the greeting message: ")
print("Good", greeting)
Federal Tax: 5
Province Tax: 9.25
How to convert one datatype into another datatype: (Type conversion)
float() function:
It is used to convert data int float.
int() function:
It is used to convert data into int.
str() function:
It is used to convert data into string.
Example of input(), type conversion and different datatypes: Example 6
productName = input("Enter the product name: ")
productPrice = float(input("Enter the product price: "))
federalTax = (productPrice * 5) /100
provinceTax = (productPrice * 9.25) /100
totalAmount = productPrice + federalTax + provinceTax
print("Product Name:",productName)
print("Price:",productPrice)
print("Federal Tax:",federalTax)
print("Province Tax:",provinceTax)
print("Total Payable amount:",totalAmount)
Operators:
1. Arithmetic operators:
2. Comparison operators(Relational operators):
3. Logical operators:
1. Arithmetic operators:
Addition +
subtraction -
division /
Multiplication *
reminder %
Example 7: Generate a pay slip:
Name of employee, Salary, federalTax
employeeId = input("Enter the employee id: ")
employeeName = input("Enter the employee name: ")
salary = float(input("Enter the salary: "))
federalTax = (salary * 11.89) / 100
provinceTax = (salary * 14.6) / 100
employeeInsurance = (salary * 0.74) / 100
quebecPensionPlan = (salary * 3.8) / 100
quebecParentalInsurancePlan = (salary * 0.46) / 100
deductions = federalTax + provinceTax + employeeInsurance + quebecPensionPlan +
quebecParentalInsurancePlan
netPay = salary - deductions
print("Employee ID:",employeeId)
print("Employee Name:",employeeName)
print("Salary:",salary)
print("Federal Tax:",federalTax)
print("Province Tax:",provinceTax)
print("Employee Insurance:",employeeInsurance)
print("Quebec Pension Plan:",quebecPensionPlan)
print("Quebec Parental Insurance Plan:",quebecParentalInsurancePlan)
print("Total Deductions:",deductions)
print("Net Pay:",netPay)
Conditional statements:
1. Simple if condition
2. If else condition
3. If else if condition
4. Nested if condition
1. Simple if condition
- You can specify only one condition.
- You can specify only one outcome.
syntax:
if condition:
statements if condition is true
Comparison operators(Relational operators):
> - greater than
< - less than
>= - greater than or equal to
<= - less than or equal to
== - equal to
!= - not equal to
Driving license:
condition: age>=16
Example 8: simple if condition with relational operators
age = int(input("Enter the age: "))
if age>=16:
print("You can apply for driving license")
print("Thank you for using my app")
2. If else condition
- You can specify two outcomes.
syntax:
if condition:
statements if condition is true
else:
statements if condition is false
Example 9: If else condition with relational operators
age = int(input("Enter the age: "))
if age>=16:
print("You can apply for driving license")
else:
print("You can't apply for driving license")
print("Thank you for using my app")
3. if..else..if condition:
- You can specify more than one condition.
- Number of outcomes depend on the number conditions that you specify in the code.
syntax:
if condition1:
statements if condition1 is true
elif condition2:
statements if condition2 is true
elif condition3:
statements if condition3 is true
else:
statements if all conditions are false
Example 10: if..else..if condition with relational operators
firstNumber = float(input("Enter the first number: "))
secondNumber = float(input("Enter the second number: "))
if firstNumber==secondNumber:
print("Both numbers are same")
elif firstNumber>secondNumber:
print("first number is largest number")
else:
print("second number is largest number")
4. Nested if condition:
- You can specify the condition inside the condition.
syntax:
if condition1:
if condition2:
statements if condition1 and condition2 are true
else:
statements if condition1 is true but condition2 is false
else:
statements if condition1 is false
Write code to take input as number and check whether number is odd or even.
odd numbers: 1 3 5 7
even numbers: 2 4 6 8 10
Example 11: Nested if with relational operators
number = int(input("Enter the number: "))
if number>0:
if number%2==0:
print("Even Number")
else:
print("Odd Number")
else:
print("Invalid number")
Logical Operators:
and, or and not
- You can specify more than two conditions together.
logical table of and operator:
condition1 condition2 output
true false false
false true false
true true true
false false false
logical table of or operator:
condition1 condition2 output
true false true
false true true
false false false
true true true
logical table of not operator:
condition output
true false
false true
Write code to print greeting based on time.
4-12 - Good Morning
13-17 - Good Afternoon
18-21 - Good Evening
>21 or <4 - Good Night
Example 12: if..elseif condition with and or operator
from datetime import datetime
currentDate = datetime.now()
currentHour = int(currentDate.strftime('%H'))
print(currentHour)
if currentHour>=4 and currentHour<=12:
print("Good Morning")
elif currentHour>=13 and currentHour<=17:
print("Good Afternoon")
elif currentHour>=18 and currentHour<=21:
print("Good Evening")
elif currentHour>21 or currentHour<4:
print("Good Night")
Deloitte wants to give appraisal to their employees except one
department(production).
Example 13: if..else condition with not operator
department = input("Enter the department name: ")
if not department=="production":
print("Employee will get appraisal.")
else:
print("Employee won't get appraisal.")
Exercises:
1. Write code to take input as password and retype password. Print the message
"User has successfully reset password" if both
passwords are same otherwise display the message "Please enter same password."
2. Write code to print salary slip in quebec province based on salary brackets.
3. Write code to take 4 numbers and display the largest one.
n1 = 35
n2 = 45
n3 = 25
n4 = 12