0% found this document useful (0 votes)
8 views

Python Programming Language

Uploaded by

Rohit Shaw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Programming Language

Uploaded by

Rohit Shaw
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Python Programming

Language
Variables
&
Data Types
Variables:

a = 10

price = 15.5

Data Types:

Numeric Integer 20, 25, 10002, 50377


Numeric Float 2.56, 7.425, 23.10002
Text String John, Max, Alex
Boolean Bool True, False
Variable Declaration

a = 10 # to check data type of variable : type(a)

price = 15.5

name = “John” or name = ‘John’

is_valid = True

Type Casting:

a = str(5). #Casting “a” as String (“5”)

a = int(5) #Casting “a” as Integer (5)

a = float(5) #Casting “a” as String (5.0)


Assigning Multiple Values:

a, b, c = "Value1", "Value2", "Value3"

Output: a = Value1, b = Value2 , c = Value3

One Value to Multiple Variables:

a = b = c = “Value”

Output: a = Value, b = Value, c = Value


Print output:

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()

input(“What is your name? ”)

Save & Use Input from User:

name = input(“What is your name? ”)

age = input(“What is your age? ”)

print(name, “ is ”, age, “ years old.”)

Format method:

print( f “{name} is {age} years old”)


age = input(“What is your age? ”)

age_after_a_decade = age + 10

Type Conversion:

age = int(input("What is your age? "))

age_after_a_decade = age + 10

age = input("What is your age? ")

age_after_a_decade = int(age) + 10
Conditional
Statement

If else & Nested If else


If Statement:

if condition_1:

Code_to_execute if condition_1 is True

If - else Statement:

if condition_1:

Code_to_execute if condition_1 is True

else:

Code_to_execute if condition_1 is False


Write a program that checks if a number entered by the user is positive.

number = int(input("Enter a number: "))

if number > 0:
print("The number is positive.")

Write a program that checks if a number entered by the user is positive or negative.

number = int(input("Enter a number: "))

if number > 0:
print("The number is positive.")
else:
print("The number is negative.")
Comparison Operators:

== Equal x == y

Not equal x != y
!=

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Logical Operators:

Returns True, only if both


and x < 10 and y>15
conditions are True

Returns True, if either of the


or x < 10 or y>15
conditions are True

Returns the opposite


not not(x > y)
Boolean value
Write a Python program that checks whether a user is logged in or not using a boolean
variable. If the user is logged in, print “Welcome, User!” Otherwise, print “Please log in.”

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.

age = int(input("Enter your age: "))

if age >= 18:

print("You are eligible to vote.")

else:

print("You are not eligible to vote.")


If - elif - else Statement:

if condition_1:

Code_to_execute if condition_1 is True

elif condition_2:

Code_to_execute if condition_1 is False and condition_2 is True

else:

Code_to_execute if condition_1 & condition_2 both are False


Write a Python program to input the cost price and selling price of a product and
determine whether the transaction resulted in a profit or loss.

cost_price = float(input("Enter the cost price: "))

selling_price = float(input("Enter the selling price: "))

if selling_price > cost_price:


print("Profit")

elif selling_price < cost_price:


print("Loss")

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

if revenue_this_year > revenue_last_year:


print("Revenue has increased.”)

elif revenue_this_year < revenue_last_year:


print("Revenue has decreased.")

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.

salary = float(input("Enter the salary: "))


years_of_service = int(input("Enter years of service: "))

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:

Code_to_execute if condition_1 is True

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 the person is under 12 years old, the ticket costs ₹50.


• If the person is between 12 and 18 years old, check:
• If they are a member, the ticket costs ₹80.
• If they are not a member, the ticket costs ₹100.
• If the person is 18 or older, check:
• If they are a member, the ticket costs ₹120.
• If they are not a member, the ticket costs ₹150.
age = int(input("Enter your age: "))
is_member = input("Are you a member? (yes/no): ")

if age < 12:


print("Your ticket costs ₹50.")

elif 12 <= age <= 18:


if is_member.lower() == "yes":
print("Your ticket costs ₹80.")
else:
print("Your ticket costs ₹100.")
else:
if is_member.lower() == "yes":
print("Your ticket costs ₹120.")
else:
print("Your ticket costs ₹150.")
Write a program that calculates the discount based on the purchase amount and whether
the customer is a loyal member:

• If the purchase amount is above ₹5,000:


• If the customer is a loyal member, they get a 15% discount.
• If the customer is not a loyal member, they get a 10% discount.

• If the purchase amount is ₹5,000 or less:


• If the customer is a loyal member, they get a 5% discount.
• If the customer is not a loyal member, they get no discount.
purchase_amount = float(input("Enter the purchase amount: "))
is_loyal_member = input("Are you a loyal member? (yes/no): ")

if purchase_amount > 5000:

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.")

You might also like