python start point
basic syntax
python variables and datatypes
python operators
conditional workings with if, else and elif
iteration using for loop
basic syntax and working of python functions
In Python, nested loops are like loops within loops—loopception! 🔄 This concept lets you dive into deeper iterations, perfect for exploring multi-layered data or repeating operations within operations. It's a looping magic trick for navigating intricate structures. Ready to unravel the power of nested loops? 🚀
a_number = 59
if a_number % 2 == 0:
print('{} is divisible by 2'.format(a_number))
elif a_number % 3 == 0:
print('{} is divisible by 3'.format(a_number))
elif a_number % 5 == 0:
print('{} is divisible by 5'.format(a_number))
else:
print('All conditions failed!')
print('{} is not divisible by 2, 3 or 5'.format(a_number))
All conditions failed!
59 is not divisible by 2, 3 or 5
Ever heard of a magic wand for quick condition checks in Python? Meet the shorthand if conditional expression! ✨🎩 This nifty one-liner lets you assign values based on a condition without the fuss of a full-blown if-else statement. Take a peek:
result = 'even' if a_number % 2 == 0 else 'odd'
Here, the result gets the label 'even' if a_number is divisible by 2; otherwise, it becomes 'odd'. It's a concise way to make decisions on the fly. Ready to sprinkle some Python magic into your code? 🔮
The pass statement in Python is a placeholder that does nothing, allowing you to acknowledge a code block without providing any specific instructions.
if a_number % 2 == 0:
pass
elif a_number % 3 == 0:
print('{} is divisible by 3 but not divisible by 2'.format(a_number))
9 is divisible by 3 but not divisible by 2
In Python, a while loop repeatedly executes a block of code as long as a specified condition is true. 🔄💡 For instance, consider this snippet:
a_number = 59
result = 1
i = 1
while i <= 5:
result = result * i
i = i + 1
print('The factorial of 5 is: {}'.format(result))
In Python, nested loops involve placing one loop inside another, allowing for more intricate iterations.
for i in range(3):
for j in range(2):
print(f'({i}, {j})')
This code produces pairs of values (i, j) for each combination of the outer loop variable i and the inner loop variable j.
outer_count = 1
while outer_count <= 3:
inner_count = 1
while inner_count <= 2:
print(f'({outer_count}, {inner_count})')
inner_count += 1
outer_count += 1
In this example, the outer while loop runs three times, and for each iteration of the outer loop, the inner while loop runs twice, producing pairs of values (outer_count, inner_count).
(1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
In Python, the break statement allows an immediate exit from a loop, while the continue statement skips the rest of the current iteration.
for num in range(10):
if num == 5:
break # Exits the loop when num equals 5
print(num)
It prints numbers from 0 to 4 and then breaks out when num equals 5.
for num in range(5):
if num == 2:
continue # Skips the iteration when num equals 2
print(num)
This prints numbers from 0 to 4, skipping the iteration when num equals 2.
range(n) - Creates a sequence of numbers from 0 to n-1
for i in range(7):
print(i)
range(a, b) - Creates a sequence of numbers from a to b-1
for i in range(3, 10):
print(i)
range(a, b, step) - Creates a sequence of numbers from a to b-1 with increments of step
for i in range(3, 14, 4):
print(i)
👉using function enumerate()
enumerate() are very handy for iterating over lists when we need to track the index of elements while iterating.
for index, value in enumerate(a_list):
print('The value at position {} is {}.'.format(index, value))
This will print the result as;
The value at position 0 is Monday.
The value at position 1 is Tuesday.
The value at position 2 is Wednesday.
The value at position 3 is Thursday.
The value at position 4 is Friday.
You are Brock of age 55 working.
*args and **kwargs offer advanced flexibility in function design.
when using an undefined number of positional arguments.
Allows passing multiple arguments without explicitly specifying them.
Useful for dynamic or variable-length argument scenarios.
when using an undefined number of positional arguments.
Allows passing multiple arguments without explicitly specifying them.
Useful for dynamic or variable-length argument scenarios.
In Python, variable scope refers to the region or context in which a variable is defined and can be accessed. Understanding variable scope is crucial for writing maintainable and error-free code. Python has two main types of variable scope: local scope and global scope.
Local scope refers to the region within a function where a variable is defined. Variables created inside a function are local to that function and cannot be accessed outside of it. This is known as function-level scope. For example:
Global scope refers to the outermost scope, and variables defined at this level are accessible throughout the entire script or module. However, caution is needed when modifying global variables from within functions. The global keyword is used to declare a global variable inside a function. For example:
Python follows the LEGB (Local, Enclosing, Global, Built-in) scope resolution rule. When a variable is referenced, Python searches for it in the following order: local scope, enclosing functions (if any), global scope, and finally, the built-in scope.
Python Closures are the inner functions that are enclosed within the outer function. Closures can access variables present in the outer function scope.
def multiplier_of(n): #n = 5
def multiplier(x):
return x * n
return multiplier
times5 = multiplier_of(5)
times4 = multiplier_of(10)
print(times5(4) ,times4(2)
A decorator in Python adds additional responsibilities/functionalities to a function dynamically without modifying a function.
In Python, a function can be passed as an argument to another function.
def decorator_func(func):
def wrapper():
print("this is inside of wrapper")
func()
print("this is after func function")
return wrapper
def example():
print("this is printed in the example function")
ex = decorator_func(example)
ex calls and sets the reference of the returned function i.e. wrapper and executes it when called with calling signature parenthesis ex().
See how the decorator is working above code to get it run with some additional functions around the function that we applied it to. Here the function example is being run within the wrapper function of the decorator function between the two print function called around it.