Learn coding with Python !
"Embrace challenges; they nurture growth."
Complete weekly classes and earn rewards !
"Learn and Grow Together"
Learn coding with Python !
These operators are used to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and more.
| Expression | Result | Explanation |
|---|---|---|
| 7 // 2 | 3 | 7 divided by 2 is 3.5, and floor division rounds down to 3 |
| -7 // 2 | -4 | -7 divided by 2 is -3.5, and floor division rounds down to -4 |
| 7 // -2 | -4 | 7 divided by -2 is -3.5, and floor division rounds down to -4 |
| -7 // -2 | 3 | -7 divided by -2 is 3.5, and floor division rounds down to 3 |
These operators are used to compare two values. They return a boolean value (True or False).
These operators are used to combine conditional statements. They return a boolean value.
Logical operators in Python help you make decisions based on multiple conditions. They combine conditions and return either True or False. Here are the three main logical operators with examples:
The and operator returns True only if both conditions are True.
(5 > 3) and (2 < 4) results in True
Another example:
10 > 5 and 5 < 2 results in False because 5 is not less than 2.
The or operator returns True if at least one condition is True.
(5 > 3) or (2 > 4) results in True
Another example:
10 > 5 or 5 < 2 results in True because 10 is greater than 5.
The not operator returns the opposite of the condition.
not (5 > 3) results in False
Another example:
not (10 < 5) results in True because 10 is not less than 5.
These operators are used to perform bit-level operations on integers.
These operators are used to assign values to variables. They can also perform operations and assign the result.
These operators are used to compare the memory locations of two objects.
These operators are used to test if a sequence is present in an object.
| Category | Operator | Description | Example |
|---|---|---|---|
| Arithmetic | + | Addition | 5 + 2 |
| Arithmetic | - | Subtraction | 5 - 2 |
| Arithmetic | * | Multiplication | 5 * 2 |
| Arithmetic | / | Division | 5 / 2 |
| Arithmetic | // | Floor Division | 5 // 2 |
| Arithmetic | % | Modulus (remainder) | 5 % 2 |
| Arithmetic | ** | Exponentiation | 5 ** 2 |
| Comparison | == | Equal to | 5 == 2 |
| Comparison | != | Not equal to | 5 != 2 |
| Comparison | > | Greater than | 5 > 2 |
| Comparison | < | Less than | 5 < 2 |
| Comparison | >= | Greater than or equal to | 5 >= 2 |
| Comparison | <= | Less than or equal to | 5 <= 2 |
| Logical | and | Logical AND | True and False |
| Logical | or | Logical OR | True or False |
| Logical | not | Logical NOT | not True |
| Bitwise | & | Bitwise AND | 5 & 2 |
| Bitwise | | | Bitwise OR | 5 | 2 |
| Bitwise | ^ | Bitwise XOR | 5 ^ 2 |
| Bitwise | ~ | Bitwise NOT | ~5 |
| Bitwise | << | Bitwise left shift | 5 << 2 |
| Bitwise | >> | Bitwise right shift | 5 >> 2 |
| Assignment | = | Assign | x = 5 |
| Assignment | += | Add and assign | x += 5 |
| Assignment | -= | Subtract and assign | x -= 5 |
| Assignment | *= | Multiply and assign | x *= 5 |
| Assignment | /= | Divide and assign | x /= 5 |
| Assignment | //= | Floor divide and assign | x //= 5 |
| Assignment | %= | Modulus and assign | x %= 5 |
| Assignment | **= | Exponentiate and assign | x **= 5 |
| Assignment | &= | Bitwise AND and assign | x &= 5 |
| Assignment | |= | Bitwise OR and assign | x |= 5 |
| Assignment | ^= | Bitwise XOR and assign | x ^= 5 |
| Assignment | <<= | Bitwise left shift and assign | x <<= 5 |
| Assignment | >>= | Bitwise right shift and assign | x >>= 5 |
| Identity | is | Identity | x is y |
| Identity | is not | Negated identity | x is not y |
| Membership | in | Membership | 'a' in 'apple' |
| Membership | not in | Negated membership | 'b' not in 'apple' |
What is Python? High-level, interpreted, general-purpose programming language.
Why Python? Easy to read, vast libraries, and strong community support.
Setting up Python: How to install Python and set up an IDE (e.g., PyCharm, VS Code).
Python Scripts: Explain how to write and run Python scripts.
Indentation: Importance of indentation in Python as it replaces braces {} used in other languages.
a = 5
b = 3.2
c = 1 + 2j
name = "John"
greeting = 'Hello, ' + name
is_active = True
print(is_active)
x = 10
y = "Hello"
PI = 3.14159
result = 10 + 5
is_equal = (10 == 10)
print(is_equal)
is_true = True and False
Today, we're going to learn about the if condition in Python. This is a fundamental concept in programming, and I'll show you how it works with some easy-to-understand examples. Let's get started!
if ConditionFirst up, the basic if condition. This is how you check if something is true and then do something if it is.
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
Here, we're saying, "If x is greater than 5, then print 'x is greater than 5'." Since x is 10, which is indeed greater than 5, it will print that message.
if-else ConditionWhat if we want to do something if the condition isn't true? We use else.
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
In this case, x is 3, which is not greater than 5, so it prints "x is not greater than 5".
if-elif-else ConditionSometimes, you have multiple conditions to check. That's where elif comes in.
x = 5
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
Here, if x is greater than 5, it prints the first message. If x equals 5, it prints the second message. Otherwise, it prints the third message. Since x is 5, it will print "x is equal to 5".
You can also check multiple conditions at once using and.
x = 7
if x > 5 and x < 10:
print("x is between 5 and 10")
Here, x is 7, which is between 5 and 10, so it prints "x is between 5 and 10".
if StatementsYou can put if statements inside other if statements. This is called nesting.
x = 8
if x > 5:
print("x is greater than 5")
if x < 10:
print("x is also less than 10")
First, it checks if x is greater than 5, and it is, so it prints the first message. Then it checks if x is less than 10, which is also true, so it prints the second message.
elif and elseLet's look at a more detailed example.
age = 25
if age < 13:
print("You are a child.")
elif age < 20:
print("You are a teenager.")
elif age < 30:
print("You are a young adult.")
else:
print("You are an adult.")
Output:
You are a young adult
Here, depending on the value of age, it prints different messages. Since age is 25, it prints "You are a young adult."
And that's it! Those are the basics of the if condition in Python. Practice these examples, and you'll get the hang of it in no time. If you found this video helpful, don't forget to like, subscribe, and hit the bell icon for more programming tutorials. Thanks for watching, and see you next time!
The if condition in Python is used to execute a block of code only if a specified condition is true. It's a fundamental part of control flow in programming, allowing your program to make decisions based on given conditions.
if x > 0:
print("x is positive")
The for in range loop in Python is a common way to iterate over a sequence of numbers. The range() function generates a sequence of numbers, and the for loop iterates over that sequence. Basic Syntax for i in range(n): i: This is the loop variable that takes on each value in the range sequence.
for i in range(5):
print(i)
The while loop in Python is used to repeatedly execute a block of code as long as a specified condition is true. Unlike the for loop, which iterates over a sequence, the while loop continues until a condition is no longer met. Basic Syntax while condition: # code block
while x > 0:
x -= 1
In Python, a function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusability. Python gives you many built-in functions like print(), but you can also create your own functions.
def greet(name):
return f"Hello, {name}"
Calling a Function
Once a function is defined, you can call it by using its name followed by parentheses, enclosing any arguments if the function accepts them.
greet("Alice")
In Python, the collections module provides alternatives to Python's general-purpose built-in containers like dictionaries, lists, sets, and tuples. These specialized container data types are useful for various tasks, offering more functionality and efficiency for certain use cases.
Creating a List
You can create a list by placing a comma-separated sequence of items within square brackets [].
In Python, a list is a built-in data type used to store collections of items. Lists are versatile, allowing you to store a sequence of items of any type, including integers, strings, and even other lists. Lists are ordered, mutable, and can contain duplicate elements.
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
To create a tuple, you use parentheses () with items separated by commas:
A tuple in Python is like a list, but you cannot change its content once it's created. Think of it as a box where you can put different items, but once the box is sealed, you can't add, remove, or change anything inside.
Key Points about Tuples:
Immutable: You can't change the items in a tuple.
Ordered: Items have a specific order, and this order won't change.
Can hold different types: You can mix and match different kinds of items (numbers, strings, etc.).
coordinates = (10, 20)
A dictionary in Python is a collection of key-value pairs. Each key is unique and is used to access its corresponding value.
Key Features
Unordered: Items don’t have a fixed order.
Mutable: You can change, add, or remove items.
Key-Value Pairs: Each item consists of a key and a value.
Creating a Dictionary
You can create a dictionary using curly braces {} with key-value pairs separated by colons.
person = {"name": "John", "age": 30}
In Python, a set is a collection of unique items. Sets are unordered and do not allow duplicate elements. They are useful for storing distinct elements and performing common mathematical set operations like unions, intersections, and differences.
Key Features of Sets
Unordered: Items have no specific order.
Unique Elements: Duplicate items are not allowed.
Mutable: You can add or remove items.
Creating a Set
You can create a set using curly braces {} or the set() function.
unique_numbers = {1, 2, 3, 4, 5}
user_name = input("Enter your name: ")
print("Hello, World!")
# This is a comment
"""
This is a multi-line comment.
"""
In Python, try and except are used for handling exceptions, which are errors that occur during the execution of a program. By using try and except, you can write code that handles these errors gracefully, preventing the program from crashing and allowing you to provide a meaningful response or take corrective action.
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
In Python, importing modules allows you to use functions, classes, and variables defined in other files. Modules help you organize your code into separate files, making it more manageable and reusable.
import math
print(math.sqrt(16))
Importing Specific Items
You can import specific functions, classes, or variables from a module using the from ... import ... syntax.
from math import pi
print(pi)
Unlock Your Potential with Clisto.in courses
Clisto is a versatile task management tool designed to help individuals and teams organize their tasks, projects, and deadlines efficiently.
Clisto works by allowing users to create tasks, set deadlines, assign priorities, and collaborate with team members. Users can organize tasks into lists and track their progress in real-time.
Yes, Clisto offers a free plan with basic features. However, there are also premium plans available with additional features for users who require more advanced functionalities.