<aside>

Variable declaration

n1 = 88
n2 = 1.056
n3 = "Hello My Name is.."
n4, n5 = True, False
n6 = n7 = 'A'
n1, n2 = n2, n1  # Swapping the values of n1 and n2 with each other
n8 = "Genius" if n2 == 88 else "Wtf why?"

<aside>

Mathematical operations

n1 = 10
n2 = 11.6
n1 += 3  # n1 = n1 + 3
n2 /= 3  # n2 = n2 / 3
n1 **= 2  # n1 = n1 ** 2 (ie n1 to the power of 2)
n3 = n1 // n2  # // drops the decimal part from dividing n1 by n2
n1 = n3 - n2
n3 %= 2  # returns the remainder of the operation n3 / 2
n2 = n3 * n1

<aside>

Standard output

n1 = 10
n2 = False
print("Hello Adeed", n1, sep="$")  # sep parameter defines the separator between printed values, defaulting value = space
print(n1 * n2, n2, "Hope", end="#") # end parameter defines the ending character of the whole print statement, default = \\n
print("Hi")

<aside>

Data types

n1 = 10.6
print(type(n1))
n1 = int(n1)
print(n1, type(n1), "\\n", type(True))
# True and False are treated the same as 1 and 0 respectively

x is assigned a value y is then assigned to x, so both y and x point to the same memory address Reassigning x to a different value does not affect y; they are now independent If a method is called on x that modifies its value, both y and x will reflect the updated value

</aside>


<aside>

Standard input

name = input("What is your name: ")
print(name, type(name), sep="\\t")

<aside>

Useful standalone functions

#eval() => can evaluate math expressions, data-type conversion, python code execution
math_expr = eval("10 + (11 - 5)")
print(math_expr)
print(type(eval("True")))
eval(input("Enter a simple python code: "))

#len() => returns the length of an iterable object
print(len("Heeeeeeeeeee"), len(str(10.7)), sep=", ")
print(len([1, 0, -8, True, "Hello"]))

#sorted() => takes an interable and returns the sorted form of that iterable in the form of a list
print(sorted(("Adeed 10", "Syed 20", "Fraah 18"), key=lambda x: int(x.split(" ")[1]), reverse=True))

#globals() => returns the [symbol table](<https://syed-adeed.notion.site/1114dc5ff5df809d895fee0f26837ea2>) of the python file
global_variable = 10
# the symbol table entry for global_variable will be "global_variable": memory address where 10 is stored
globals().update({"global_variable": 11})
print(global_variable)

print(ord("A"))  # converts a symbol to its ascii value
print(chr(65))  # converts the ascii value to the symbol

#The slice() function
x = slice(0, 10, 2)
# now you can use x to get a sub section of any iterable(ex string, list, etc)
# syntax => iterable_name[x], this is the same as saying iterable_name[0:10:2]

#sum(), max(), min(), round(), any(), all(), zip()
# map(lambda func, iterable...) applies func to each element
# filter(func x: True/False, iterable), reduce(func a,b: a +b, iterable, initial_value), enumerate()=> index, value

<aside>

Boolean expressions

expression = (10 == 19 or 15 != 19) and "5" not in "108" and (type(4) is int) and (5 > 4 and 67 >= 9 or 9 < 3 or 5 <= 5)
print(expression)

<aside>

“is” v/s “==”

# == compares the equivalence of objects
# is checks if the objects are sharing the same memory address

n1, n2 = [], []
print("n1 is n2:", n1 is n2)
print("n1 == n2:", n1 == n2)

# apparently what (n1 is n2) is doing
print("id(n1) == id(n2):", id(n1) == id(n2))

# The id/memory location will change everytime you run the code. (Note = Not during runtime)
# since n1 and n2 are 2 different objects, therefore they have different memory location

n1 = n2  # now n1 and n2 are sharing the same memory address
print("n1 = n2; n1 is n2:", n1 is n2)

<aside>

Conditionals

# Conditional statements are used to execute code based on the result of a boolean expression

user_input = "hello"
is_valid = True
is_authenticated = True
user_role = "admin"
access_level = "read"

if is_valid:
    if is_authenticated:
        print("User is authenticated")
    elif user_input == "secret":
        if user_role == "admin":
            print("Admin access granted")
        elif user_role == "user":
            print("User access granted")
        else:
            print("Role not recognized")
    elif user_input == "exit":
        match access_level:  # like switch block in c/cpp
            case "read" | "write":  # "|" is the "or" operator
                print("Exiting with limited permissions")
            case "admin":
                print("Exiting with admin permissions")
            case _:  # "_" is a wildcard entry its like the final else block
                print("Exiting without specific permissions")
else:
    if user_input == "retry":
        print("Please try again")

<aside>

Loops

i, value = 0, 0

while i < 5 and value < 25:
    value += 2
    for j in range(5, 11, 2):
        # for loop takes an iterable object to iterate over
        if j == 2:
            continue  # skips the rest of the code block and loop moves to next iteration
        value += 1
    for _ in "Hello":
        pass  # does nothing, acts as a placeholder
    i += 1
    if value > 30:
        break  # stops the execution of the loop in the scope of where its called
else:
    # else block is optional, can also be used with a for loop
    # the code inside it will only execute if loop block isnt interrupted by break statement
    value += 10

print(value)