diff --git a/python_for_coding_interviews/1_variables.py b/python_for_coding_interviews/1_variables.py new file mode 100644 index 0000000..80e720a --- /dev/null +++ b/python_for_coding_interviews/1_variables.py @@ -0,0 +1,28 @@ +""" +In python , variables are dynamically typed. +It means their data type is determined at runtime. +No need to specify their datatype like we do in c++ e.g. int x; +""" + +n = 0 +print('n =', n) +# n = 0 + +n = "abc" +print('n =', n) +# n = abc + +# Multiple assignments +n, m = 0, "abc" +n, m, z = 0.125, "abc", False + +# Increment +n = n + 1 # good +n += 1 # good +# n++ # bad; there is no such operator in python + +# None is null (absence of value) +n = 4 +n = None +print("n =", n) +# n = None diff --git a/python_for_coding_interviews/2_if_statements.py b/python_for_coding_interviews/2_if_statements.py new file mode 100644 index 0000000..309a8b9 --- /dev/null +++ b/python_for_coding_interviews/2_if_statements.py @@ -0,0 +1,24 @@ +""" +In python, there is no parenthesis and curly brackets. +We use indentation to specify a piece of block instead of brackets or parenthesis. +So, If statements also don't need that +""" + +n = 1 +if n > 2: + n -= 1 +elif n == 2: + n *= 2 +else: + n += 2 + +""" +But Parentheses needed for multi-line conditions. +Logical Operators +1. and = && +2. or = || +""" +n, m = 1, 2 +if ((n > 2 and + n != m) or n == m): + n += 1 diff --git a/python_for_coding_interviews/3_loops.py b/python_for_coding_interviews/3_loops.py new file mode 100644 index 0000000..c13e577 --- /dev/null +++ b/python_for_coding_interviews/3_loops.py @@ -0,0 +1,37 @@ +""" +Python have two loops, while and for loop. + +While loop is used when you don't know about the number of iterations. +Example, you have to take input from user repeatedly until it enters correct input. + +For loop is used when you know about the number of iterations. +Example, print a number 10 times. + +""" + +n = 5 +while n < 5: + print(n) + n += 1 + +""" +We use range function for iteration in for loop. +Some facts about range function: +1. By default, it starts with 0. +2. range(stop_value) equals to i < stop_value. It means last digit is always exclusive. +3. You can also specify the starting value like this => range(start, stop). +4. By default, it increments by +1. +5. You can also specify the incrementing value like this => range(start, stop, increment). +""" + +# Looping from i = 0 to i = 4 +for i in range(5): + print(i) + +# Looping from i = 2 to i = 5 +for i in range(2, 6): + print(i) + +# Looping from i = 5 to i = 2 with -1 increment +for i in range(5, 1, -1): + print(i) diff --git a/python_for_coding_interviews/4_math.py b/python_for_coding_interviews/4_math.py new file mode 100644 index 0000000..32ed875 --- /dev/null +++ b/python_for_coding_interviews/4_math.py @@ -0,0 +1,48 @@ +import math + +# Division is decimal by default +print('5 / 2 = ', 5 / 2) +# output => 2.5 + +# Double slash rounds down to integer +print('5 // 2 = ', 5 // 2) +# output => 2 + +# CAREFUL: most languages round towards 0 by default +# python rounds towards minimum value +# So negative numbers will round down +print('-3 // 2 = ', -3 // 2) +# output => -2 but it should be -1 + +# A workaround for rounding towards zero +# is to use decimal division and then convert to int. +print('-3 // 2 = ', int(-3 / 2)) +# output => -1 + +# Modding is similar to most languages +print('10 % 3 = ', 10 % 3) +# output => 1 + +# Except for negative values +print('-10 % 3 = ', -10 % 3) + +# To be consistent with other languages modulo use math fmod + +print(math.fmod(-10, 3)) + +# More math helpers +print(math.floor(3 / 2)) # rounds down +print(math.ceil(3 / 2)) # rounds up +print(math.sqrt(2)) +print(math.pow(2, 3)) + +# Max / Min Int +float("inf") +float("-inf") + +# Python numbers are infinite so they never overflow +print(math.pow(2, 200)) + +# But still less than infinity +print(math.pow(2, 200) < float("inf")) +# output => True diff --git a/python_for_coding_interviews/__init__.py b/python_for_coding_interviews/__init__.py new file mode 100644 index 0000000..e69de29