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
Working with Strings
Strings in Python have various built-in methods for manipulation and formatting:


String Methods:
.upper(): Converts all characters to uppercase.
.lower(): Converts all characters to lowercase.
.capitalize(): Capitalizes the first character.
.replace(old, new): Replaces occurrences of a substring with another substring.
.strip(): Removes whitespace from the beginning and end of a string.
.split(separator): Splits the string into a list based on the separator.
Examples:-
text = "   Python Programming   "
print(text.upper()) # Output: " PYTHON PROGRAMMING "
print(text.lower()) # Output: " python programming "
print(text.strip()) # Output: "Python Programming"
print(text.replace("Python", "Java")) # Output: " Java Programming "
print(text.split()) # Output: ['Python', 'Programming']

String Formatting:
Using % Operator:
name = "Alice"
age = 30
print("Name: %s, Age: %d" % (name, age))

Using .format() Method:
print("Name: {}, Age: {}".format(name, age))

Using f-strings (Python 3.6+):
print(f"Name: {name}, Age: {age}")
Practice Exercises for Day 2

Exercise 1: Variable Assignment
Create variables for your name, age, height, and whether you are a student. Print each variable with a descriptive message.

Exercise 2: String Operations
Write a Python program that:
Takes a string input from the user.
Prints the string in uppercase and lowercase.
Replaces a word in the string with another word.
Strips any leading or trailing whitespace.

Exercise 3: Typecasting
Write a program that:
Takes a number as a string input from the user.
Converts the string to an integer and a float.
Prints the type of each converted value.
Homework for Day 2

Variable Creation:
Write a Python program that calculates the area of a rectangle. Use variables to store the length and width. Print the area with a descriptive message.

String Manipulation:
Write a Python program that reads a full name from the user. Then, split the name into first and last names and print them separately. Also, print the total number of characters in the full name (excluding spaces).
Summary
Today, you've learned about variables, data types, and basic string operations in Python. Understanding these concepts will help you manage and manipulate data effectively in your programs.
Introduction to Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.


Python provides several types of operators:
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Bitwise Operators
Membership Operators
Identity Operators
1
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.


a = 10
b = 3

print("Addition:", a + b) # Output: 13
print("Subtraction:", a - b) # Output: 7
print("Multiplication:", a * b) # Output: 30
print("Division:", a / b) # Output: 3.333...
print("Floor Division:", a // b) # Output: 3
print("Modulus:", a % b) # Output: 1
print("Exponentiation:", a ** b) # Output: 1000
Comparison Operators
Comparison operators are used to compare two values. They return either True or False based on the condition.


x = 10
y = 5

print("x == y:", x == y) # Output: False
print("x != y:", x != y) # Output: True
print("x > y:", x > y) # Output: True
print("x < y:", x < y) # Output: False
print("x >= y:", x >= y) # Output: True
print("x <= y:", x <= y) # Output: False
1👍1
Logical Operators
Logical operators are used to combine conditional statements.


x = 10
y = 5

print("x > 5 and y < 10:", x > 5 and y < 10) # Output: True
print("x > 15 or y < 10:", x > 15 or y < 10) # Output: True
print("not(x > 5):", not(x > 5)) # Output: False
Assignment Operators
Assignment operators are used to assign values to variables.


x = 5

x += 3 # x = x + 3
print(x) # Output: 8

x *= 2 # x = x * 2
print(x) # Output: 16
👍1
Bitwise Operators
Bitwise operators are used to perform bit-level operations on integers.


x = 5  # 0101 in binary
y = 3 # 0011 in binary

print("x & y:", x & y) # Output: 1 (0001 in binary)
print("x | y:", x | y) # Output: 7 (0111 in binary)
print("x ^ y:", x ^ y) # Output: 6 (0110 in binary)
print("~x:", ~x) # Output: -6 (Inverts all bits)
print("x << 2:", x << 2) # Output: 20 (Shifts bits to the left)
print("x >> 2:", x >> 2) # Output: 1 (Shifts bits to the right)
Membership Operators
Membership operators are used to test if a sequence (like a string, list, or tuple) contains a value.


fruit = "apple"

print("a in fruit:", "a" in fruit) # Output: True
print("x not in fruit:", "x" not in fruit) # Output: True
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