From acbe8ba5cf06010af80ad1bd3aa2497b6779a824 Mon Sep 17 00:00:00 2001 From: AimenYaseen Date: Sun, 25 Jun 2023 14:06:27 +0500 Subject: [PATCH 1/7] added a new pkg "python_concepts" --- python_concepts/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 python_concepts/__init__.py diff --git a/python_concepts/__init__.py b/python_concepts/__init__.py new file mode 100644 index 0000000..e69de29 From 2bd86e99c82b85e6a04df6fac7dcccf5ff39cbbc Mon Sep 17 00:00:00 2001 From: AimenYaseen Date: Sun, 25 Jun 2023 14:07:22 +0500 Subject: [PATCH 2/7] renamed python_concepts --- {python_concepts => python_for_coding_interviews}/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {python_concepts => python_for_coding_interviews}/__init__.py (100%) diff --git a/python_concepts/__init__.py b/python_for_coding_interviews/__init__.py similarity index 100% rename from python_concepts/__init__.py rename to python_for_coding_interviews/__init__.py From 84a4cd7a69d22a7694f68c029eb8b34cec4d2b65 Mon Sep 17 00:00:00 2001 From: AimenYaseen Date: Mon, 26 Jun 2023 11:07:05 +0500 Subject: [PATCH 3/7] python variables --- python_for_coding_interviews/variables.py | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 python_for_coding_interviews/variables.py diff --git a/python_for_coding_interviews/variables.py b/python_for_coding_interviews/variables.py new file mode 100644 index 0000000..80e720a --- /dev/null +++ b/python_for_coding_interviews/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 From ecbb8b0891b617e2406b9f25dcb1dc43a9661771 Mon Sep 17 00:00:00 2001 From: AimenYaseen Date: Mon, 26 Jun 2023 11:10:56 +0500 Subject: [PATCH 4/7] python if statements --- python_for_coding_interviews/if_statements.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 python_for_coding_interviews/if_statements.py diff --git a/python_for_coding_interviews/if_statements.py b/python_for_coding_interviews/if_statements.py new file mode 100644 index 0000000..309a8b9 --- /dev/null +++ b/python_for_coding_interviews/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 From 2fa0077e8e4aea59b9fe6df7b0e3c0e2a1d89cbe Mon Sep 17 00:00:00 2001 From: AimenYaseen Date: Mon, 26 Jun 2023 11:12:51 +0500 Subject: [PATCH 5/7] renamed to specify their order --- python_for_coding_interviews/{variables.py => 1_variables.py} | 0 .../{if_statements.py => 2_if_statements.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename python_for_coding_interviews/{variables.py => 1_variables.py} (100%) rename python_for_coding_interviews/{if_statements.py => 2_if_statements.py} (100%) diff --git a/python_for_coding_interviews/variables.py b/python_for_coding_interviews/1_variables.py similarity index 100% rename from python_for_coding_interviews/variables.py rename to python_for_coding_interviews/1_variables.py diff --git a/python_for_coding_interviews/if_statements.py b/python_for_coding_interviews/2_if_statements.py similarity index 100% rename from python_for_coding_interviews/if_statements.py rename to python_for_coding_interviews/2_if_statements.py From d70ef56fc3dc716346192e8a8164465080bcc9b0 Mon Sep 17 00:00:00 2001 From: AimenYaseen Date: Tue, 27 Jun 2023 10:48:01 +0500 Subject: [PATCH 6/7] python loops --- python_for_coding_interviews/3_loops.py | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 python_for_coding_interviews/3_loops.py 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) From 862dc4f4bf80676fdfb7d399aa7fd08cf4407e1c Mon Sep 17 00:00:00 2001 From: AimenYaseen Date: Tue, 27 Jun 2023 11:04:44 +0500 Subject: [PATCH 7/7] python math problems --- python_for_coding_interviews/4_math.py | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 python_for_coding_interviews/4_math.py 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