Python Programming Language
Python Programming Language
Language
Variables
&
Data Types
Variables:
a = 10
price = 15.5
Data Types:
price = 15.5
is_valid = True
Type Casting:
a = b = c = “Value”
print(“Hello! World”)
print(a)
print(a, b, c)
print(a + b + c)
Example:
print(a, b, c)
a = 5, b = 7, c = 2
print(a + b + c)
a = 5, b = 7, c = print(a, b, c)
“2” print(a + b + c)
Print output:
print(“Hello! World”)
print(a)
print(a, b, c)
print(a + b + c)
Example:
print(a, b, c) 572
a = 5, b = 7, c = 2
print(a + b + c) 14
a = 5, b = 7, c = print(a, b, c) 572
“2” print(a + b + c) Error
Input from User:
input()
Format method:
age_after_a_decade = age + 10
Type Conversion:
age_after_a_decade = age + 10
age_after_a_decade = int(age) + 10
Conditional
Statement
if condition_1:
If - else Statement:
if condition_1:
else:
if number > 0:
print("The number is positive.")
Write a program that checks if a number entered by the user is positive or negative.
if number > 0:
print("The number is positive.")
else:
print("The number is negative.")
Comparison Operators:
== Equal x == y
Not equal x != y
!=
is_logged_in = True # This boolean variable determines if the user is logged in or not
if is_logged_in == True:
print("Welcome, User!")
else:
print("Please log in.”)
Write a program that checks if a person is eligible to vote based on their age. A person is
eligible if they are 18 or older.
else:
if condition_1:
elif condition_2:
else:
else:
print("No Profit, No Loss")
Suppose two variables “revenue_last_year”, “revenue_this_year” hold the revenue of a
business for two consecutive years 1000000 and 1500000. Write a Python program to
check whether the revenue has increased or decreased.
revenue_last_year = 1000000
revenue_this_year = 1500000
else:
print("Revenue is the same as last year.”)
A company gives a 10% bonus if the employee’s years of service are more than 5. Write a
Python program that calculates the bonus for an employee based on their service years.
if years_of_service > 5:
bonus = salary * 0.10
print(f"Bonus is: {bonus}”)
else:
print("No bonus for less than 5 years of service.")
Nested If - else Statement:
if condition_1:
elif condition_2:
if condition_2.1:
Code_to_execute if condition_2.1 is True
else:
Code_to_execute if condition_2.1 is False
else:
if condition_3.1:
Code_to_execute if condition_3.1 is True
else:
Code_to_execute if condition_3.1 is False
Write a program that determines the ticket price based on age and whether the person is a
member:
if is_loyal_member.lower() == "yes":
print("You get a 15% discount.")
else:
print("You get a 10% discount.")
else:
if is_loyal_member.lower() == "yes":
print("You get a 5% discount.")
else:
print("You get no discount.")