I'm passionate python programmer and welcome to learning python with Mubashir. This repository is designed for students, teachers and researcher who is beginner and want to excel in their field. Whether you're just starting career as passionate programmer or looking to sharpend your skills, this series provide structured, hands on learning through Jupyter Notebook covering different topics.
Strings are sequence of numbers, letters, symbols and spaces enclosed into single, double or triple quotes. Strings are ordered collection, immutable once created it cannot be changed, strings are primarily used to strore and manupulate textual data.
- Single Quote 'Hellow World'
- Double Quote "Hello World!"
- Triple Quote ''' This is your Second Assignment'''
string operations are action performed on sequence of characters (string) in programming, including cancatination, substringing, comparison, case conversion, finding length and finding specific character or sub strings. Here are some common string operations:
Joining two or more srings together to form a single, longer string. You can join two or more strings using ' + ' operator.
Extracting a portion of a string from it.
Determining the number of character from string by using the len(). It counts all number, spaces, character and symbols.
Repetition means saying the same thing again and again. Repeat a string multiple times using the * operators.
Strings are like list of characters. You can get any letter using its index starting from 0. Think of it like picking a word by its position.
You can get any part of string by using [start:end]
Use methods like .upper(),.lower() and .title() to change letter cases. It is used for formating text nicely.
In string, .strip() method used to remove extra spaces from beginning and end.
In string, .replace() method used to replace specific part of word or letter.
Use in to check one string is inside another. It is very helpful to search in text.
- .spilit() method used to breat the string into list.
- .join() method used to join the string together.
We can add values into strings f-string or .format().
Let's talk about user input in very easy way. User input means getting information from the user using the program. We use the input() function to do this in python.
age = int(input("Enter your age: "))
print(age + 5)
Operators are symbols or special words that are used to perform operatios on variable or values. a = 5 b = 3 print(a + b) # Output: 8
1. Arithmetic Operators: Arithmetic operators are used to perform basic mathematic operations.Some Common Arithmetic Operators are
| operator | Description | Example |
|---|---|---|
| + | Addition | 3 + 1 = 4 |
| - | Subtraction | 2 - 1 = 1 |
| * | Multiplication | 4 * 2 = 8 |
| / | Division | 8 / 2 = 4 |
| % | Modulus | 5 % 2 = 1 |
| ** | Exponentation | 2 ** 3 = 8 |
2.Comparison operators: Comparison operators are used to compare two values.Some Common Comparison Operators are
| operator | Description | Example |
|---|---|---|
| == | Equal to | 5 == 5 |
| != | Not Equal to | 5 != 5 |
| > | Greater than | 4 > 2 |
| < | Less than | 2 < 4 |
| >= | Greater or Equal | 6 >= 6 |
| <= | Less or Equal | 3 <= 5 |
3.Logical Operators: Logical Operators are used to combine and invert boolean values. Some Common Logical Operators are
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
&& |
Logical AND | True if both conditions are true | true && false |
false |
| ` | ` | Logical OR | True if at least one condition is true | |
! |
Logical NOT | Inverts the truth value | !true |
false |
An identity operators determines whether two variables or values refer to the same object in memory. In python, the primary identity operators are: is: This operator returns True if both operands refer to the exact same object in memory, and False otherwise. is not: This operator returns True if both operands refer to different objects in memory, and False otherwise.
Membership operators are used to test for the presence of a value within a sequence. They determine if a specific element or substring exists within a larger collection of data. These operators are commonly used in programming languages like Python. In Python, the two primary membership operators are: in operator: This operator evaluates to True if the value on its left operand is found within the sequence on its right operand. Otherwise, it evaluates to False. not in operator: This operator is the inverse of in. It evaluates to True if the value on its left operand is not found within the sequence on its right operand. Otherwise, it evaluates to False.
A bitwise operator performs a logical or bitwise operation on the individual bits of integers. Common bitwise operators include AND (&), OR (|), XOR (^), and NOT (), which act on the binary representations of numbers to manipulate them at the bit level for tasks like setting flags, encryption, and data compression.):** Inverts each bit (0 becomes 1, and 1 becomes 0).
Left Shift (<<): Shifts the bits of a number to the left by a specified number of positions, filling the rightmost positions with zeros.
Right Shift (>>): Shifts the bits of a number to the right, filling the leftmost positions according to the type of shift (arithmetic or logical).
Common Bitwise Operators:
Bitwise AND (&): Returns 1 only if both corresponding bits are 1.
Bitwise OR (|): Returns 1 if at least one of the corresponding bits is 1.
Bitwise XOR (^): Returns 1 if the corresponding bits are different (one is 0 and the other is 1).
**Bitwise NOT (
| Operator | Name | Description | Example (a = 5, b = 3) | Result (Decimal) | Result (Binary) |
|---|---|---|---|---|---|
& |
AND | Sets each bit to 1 if both bits are 1 | a & b → 5 & 3 |
1 | 0001 |
| ` | ` | OR | Sets each bit to 1 if one of the bits is 1 | `a | b→5 |
^ |
XOR | Sets each bit to 1 if bits are different | a ^ b → 5 ^ 3 |
6 | 0110 |
~ |
NOT | Inverts all the bits | ~a → ~5 |
-6 | ...11111010 |
<< |
Left Shift | Shifts bits to the left, adds 0s on the right | a << 1 → 5 << 1 |
10 | 1010 |
>> |
Right Shift | Shifts bits to the right, discards rightmost | a >> 1 → 5 >> 1 |
2 | 0010 |
Data types in python define the type of value a variable holds _______ whether it's number, text, list, etc.
Some common built in data types in python are given below.
Numeric data type represent different type of numbers such as Int, float and complex.
Integer data type represents whole number which is stored in variable.
Float data type represents decimal number which is stored in variable.
Complex data type represents complex number — number with real part and imaginary part.
Some common math functions you can use in Python’s built-in math module, with examples.
| Function | Description | Example | Output |
|---|---|---|---|
math.sqrt(x) |
Square root | math.sqrt(16) |
4.0 |
math.pow(x, y) |
x raised to the power y | math.pow(2, 3) |
8.0 |
math.ceil(x) |
Smallest integer ≥ x | math.ceil(4.2) |
5 |
math.floor(x) |
Largest integer ≤ x | math.floor(4.7) |
4 |
math.sin(x) |
Sine of x (x in radians) | math.sin(math.pi/2) |
1.0 |
math.cos(x) |
Cosine of x (x in radians) | math.cos(0) |
1.0 |
math.tan(x) |
Tangent of x (x in radians) | math.tan(math.pi/4) |
1.0 |
math.log(x[, base]) |
Logarithm of x (default base e) | math.log(100, 10) |
2.0 |
math.exp(x) |
Exponential function (e^x) | math.exp(2) |
7.389056... |
math.factorial(n) |
Factorial of n (n!) | math.factorial(5) |
120 |
The if-elif-else statement is a fundamental control flow structure in many programming languages, including Python, used for making decisions and executing different blocks of code based on conditions.
-
if — Tests a condition; runs code block if condition is True.
-
elif — “else if” — tests another condition if previous if or elif was False.
-
else — Runs if all previous conditions were False.
A nested statement in Python refers to placing one or more statements inside another statement.
age = 25
is_student = True
if age >= 18:
print("You are an adult.")
if is_student:
print("You are also a student.")
else:
print("You are not a student.")
else:
print("You are a minor.")
In Python, the shorthand if-else statement is also known as a conditional expression or ternary operator. It allows you to write a concise if-else statement on a single line, returning a value based on a condition
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)
It is control flow statement in python which allows code to be executed repeatedly as long as the condition is true.
Syntax
While condition:
If the condition never becomes false, then the loop will run forever called and infinite loop.
A for loop in python is used to iterate over a sequence and execute block of code for each item in iteration.
Syntax
for item in sequence:
A loop inside another loop is called nested loop. You can nest for loops, while loops, or mix them.
Syntax
For outer in outer sequence:
For inner in inner sequence:
Loop control statements help you change the normal flow of loop. They help you:
- Break the loop
- Exit the certain iteration of loop
- Add placeholder
Break statement stops the loop completely, even if the condition is still True.
Continue statement skips the current iteration and move to the next one.
Pass statement does nothing. It’s used when a statement is syntactically required but you don’t want any action.
Range function in python is used to generate a sequence of numbers, which is commonly used in loops especially for loops. The purpose of range function is to provide a sequence of numbers to iterate over in loop.
Syntax
range(stop)
range(start, stop)
range(start, stop, step)
In python, for loop and while loop can have an else clause which is executable only if the loop is not terminated by break statement.
Syntax
for item in iterable:
***loop body***
if condition:
break
else:
*** runs only if the loop wasn't broken***
Syntax
while condition:
***loop body***
if condition_to_break:
break
else:
*** runs only if loop ended normally (not by break)***