Free Python Course | Programming Tutorials
599 subscribers
9 photos
2 files
6 links
Join Noob to Pro in Python in 100 Days! 🚀 Learn Python with daily lessons, practical examples, free code samples, exercises, and homework. Ideal for beginners and those looking to advance their skills. Start your journey to becoming a Python pro! 🐍
Download Telegram
Identity Operators
Identity operators are used to compare the memory locations of two objects.


x = [1, 2, 3]
y = [1, 2, 3]
z = x

print("x is y:", x is y) # Output: False (Different objects)
print("x is z:", x is z) # Output: True (Same object)
print("x is not y:", x is not y) # Output: True
Expressions and Operator Precedence
Expressions are combinations of values and operators that are interpreted to produce some other value. Python follows a specific order of operations (precedence) similar to mathematics.


Operator Precedence:
Parentheses ()
Exponentiation **
Multiplication and Division *, /, //, %
Addition and Subtraction +,
Practice Exercises for Day 3

Exercise 1: Arithmetic Operations
Write a program that takes two numbers from the user and performs all arithmetic operations on them (addition, subtraction, multiplication, division, floor division, modulus, and exponentiation). Print the results in a formatted way.


Exercise 2: Comparison Operations
Write a Python program to compare two numbers provided by the user using comparison operators. Print whether the first number is greater, less, or equal to the second number.


Exercise 3: Logical Operations
Write a Python program that takes three boolean inputs from the user (using input() function and converting them to boolean using bool() function) and performs logical and, or, and not operations on them. Print the results.


Exercise 4: Membership and Identity Operators
Write a program to check if a given element is in a list. Also, use the identity operator to check if two variables point to the same list object.


Exercise 5: Bitwise Operations
Write a Python program to demonstrate the use of bitwise operators (&, |, ^, ~, <<, >>) on two integer inputs provided by the user. Print the binary and decimal results for each operation.
Homework for Day 3

Simple Calculator Program:
Write a Python program that behaves like a simple calculator. It should take three inputs from the user: two numbers and an operator (+, -, *, /, %, //, **). Based on the operator provided, it should perform the corresponding arithmetic operation and display the result. Include error handling for division by zero.


Area and Perimeter Calculator:
Write a Python program that takes the length and width of a rectangle from the user and calculates the area and perimeter. Also, provide options to calculate the area and circumference of a circle by taking the radius as input.


Number Guessing Game:
Create a simple number guessing game where the program randomly selects a number between 1 and 100, and the user has to guess it. The program should give hints (like "Too high!" or "Too low!") and count the number of attempts taken to guess the correct number. Use comparison and logical operators to manage the game's flow.


Operators Practice:
Write a Python program that takes an input string from the user and checks if the string is a palindrome (a word, phrase, or sequence that reads the same backward as forward). Use string manipulation and comparison operators to achieve this.
Summary

Today, we covered various types of operators in Python, including arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators. We also discussed operator precedence and how Python evaluates expressions.

Arithmetic Operators perform basic mathematical operations.
Comparison Operators compare values and return True or False.
Logical Operators combine multiple conditions.
Assignment Operators are used to assign and modify variable values.
Bitwise Operators operate at the binary level.
Membership Operators check for membership within a sequence.
Identity Operators check if two variables reference the same object.

Understanding these operators and how to use them correctly will help you build more complex and logical Python programs. Practice using these operators with different data types and expressions to become more proficient in Python!
What is the output of the following code snippet: a = 10; b = 5; print(a - b)?
Anonymous Quiz
8%
15
86%
5
4%
10
1%
0
What will be the output of the following code snippet: x = 4; y = x**2; z = y // 3; print(z)?
Anonymous Quiz
10%
1
20%
2
49%
5
21%
16
👎1
Which expression would correctly swap the values of two variables, a and b, without using a third variable?
Anonymous Quiz
22%
a, b = a, b
35%
a, b = b, a
22%
a = b; b = a
22%
a = a + b; b = a - b; a = a - b
🔥1
Introduction to Conditional Statements

Conditional statements allow us to execute different blocks of code based on certain conditions. In Python, the primary conditional statements are:

1. if statement
2. elif statement
3. else statement

These statements work together to check for conditions and execute code accordingly.
👍1
The if Statement
The if statement is used to test a condition. If the condition is True, the block of code under the if statement is executed. Otherwise, the block of code is skipped.


Syntax
if condition:
# Code to execute if condition is True

Example
age = 18

if age >= 18:
print("You are eligible to vote.")
The else Statement
The else statement is used to execute a block of code if the if condition is False.


Syntax
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False


Example
age = 16

if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
The elif Statement
The elif (short for "else if") statement allows you to check multiple conditions. If the first if condition is False, it checks the elif condition. If the elif condition is True, the code block under elif is executed. You can have multiple elif statements.


Syntax
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if all conditions are False


Example
marks = 75

if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
else:
print("Grade: D")
Nested if Statements
You can have if statements inside other if statements. This is called nesting. Nested if statements are useful when you need to check multiple conditions at different levels



Syntax
if condition1:
# Code to execute if condition1 is True
if condition2:
# Code to execute if condition2 is True
else:
# Code to execute if condition2 is False
else:
# Code to execute if condition1 is False


Example
number = 15

if number > 10:
print("Number is greater than 10.")
if number % 2 == 0:
print("Number is even.")
else:
print("Number is odd.")
else:
print("Number is 10 or less.")
Combining Conditions with Logical Operators
You can combine multiple conditions using logical operators like and, or, and not to form complex conditions.

- and Operator: Returns True if both conditions are True.
- or Operator: Returns True if at least one of the conditions is True.
- not Operator: Reverses the result of the condition.


Example
age = 20
has_voter_id = True

if age >= 18 and has_voter_id:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Conditional Expressions (Ternary Operator)
Python supports a shorter syntax for writing if...else statements using conditional expressions, also known as the ternary operator.


Syntax
value_if_true if condition else value_if_false


Example
age = 18
status = "Eligible" if age >= 18 else "Not eligible"
print(status) # Output: Eligible
Practice Exercises for Day 4

Exercise 1: Voting Eligibility

Write a Python program that takes the age of a person as input and checks whether the person is eligible to vote or not. (Age should be 18 or above to be eligible.)


Exercise 2: Grading System
Write a Python program that takes the marks of a student as input and prints the grade based on the following criteria:
Marks >= 90: Grade A
Marks >= 80: Grade B
Marks >= 70: Grade C
Marks >= 60: Grade D
Marks < 60: Grade F


Exercise 3: Leap Year Checker
Write a Python program to check if a given year is a leap year or not. A leap year is exactly divisible by 4, except for years that are exactly divisible by 100. However, years divisible by 400 are leap years.


Exercise 4: Odd or Even
Write a Python program that takes a number as input and checks whether the number is odd or even.


Exercise 5: Calculator Using if-elif-else
Create a simple calculator that takes two numbers and an operator (+, -, *, /) as input and performs the corresponding operation using if-elif-else statements.
👍1
Homework for Day 4

BMI Calculator:

Write a Python program that takes the weight (in kg) and height (in meters) of a person as input and calculates their Body Mass Index (BMI). The program should then categorize the BMI as:
BMI < 18.5: Underweight
18.5 <= BMI < 24.9: Normal weight
25 <= BMI < 29.9: Overweight
BMI >= 30: Obesity


Number Classification:
Write a Python program that takes an integer as input and checks if it is positive, negative, or zero

Password Strength Checker:
Create a Python program that takes a password as input and checks its strength based on the following criteria:
At least 8 characters long
Contains both uppercase and lowercase characters
Contains at least one digit
Contains at least one special character (e.g., @, #, !)


Nested if Example:
Write a Python program that takes a person's age and income as input and prints whether the person has to pay taxes. The tax rules are:
If the age is less than 18, they do not need to pay taxes.
If the age is between 18 and 60, they need to pay taxes if their income is more than 5 lakh.
If the age is above 60, they need to pay taxes if their income is more than 3 lakh.
👍3
Summary

Today, we covered conditional statements in Python, which are crucial for controlling the flow of a program. We learned about:

if, elif, and else statements: Used for decision-making in Python.
Nested if statements: Allow for more complex decision-making processes.
Combining conditions using logical operators (and, or, not) to create complex expressions.
Conditional expressions (ternary operators): A concise way to write if...else statements.

Understanding and mastering these concepts will help you write programs that can make decisions based on user input or other factors, making your code more dynamic and responsive.
👍4
Which keyword is used to check a condition in Python?
Anonymous Quiz
8%
for
77%
if
8%
while
6%
switch