Skip to content

Commit 6289be1

Browse files
authored
Merge pull request #2 from AimenYaseen/python-concepts
Python concepts
2 parents ff832cf + 862dc4f commit 6289be1

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
In python , variables are dynamically typed.
3+
It means their data type is determined at runtime.
4+
No need to specify their datatype like we do in c++ e.g. int x;
5+
"""
6+
7+
n = 0
8+
print('n =', n)
9+
# n = 0
10+
11+
n = "abc"
12+
print('n =', n)
13+
# n = abc
14+
15+
# Multiple assignments
16+
n, m = 0, "abc"
17+
n, m, z = 0.125, "abc", False
18+
19+
# Increment
20+
n = n + 1 # good
21+
n += 1 # good
22+
# n++ # bad; there is no such operator in python
23+
24+
# None is null (absence of value)
25+
n = 4
26+
n = None
27+
print("n =", n)
28+
# n = None
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
In python, there is no parenthesis and curly brackets.
3+
We use indentation to specify a piece of block instead of brackets or parenthesis.
4+
So, If statements also don't need that
5+
"""
6+
7+
n = 1
8+
if n > 2:
9+
n -= 1
10+
elif n == 2:
11+
n *= 2
12+
else:
13+
n += 2
14+
15+
"""
16+
But Parentheses needed for multi-line conditions.
17+
Logical Operators
18+
1. and = &&
19+
2. or = ||
20+
"""
21+
n, m = 1, 2
22+
if ((n > 2 and
23+
n != m) or n == m):
24+
n += 1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Python have two loops, while and for loop.
3+
4+
While loop is used when you don't know about the number of iterations.
5+
Example, you have to take input from user repeatedly until it enters correct input.
6+
7+
For loop is used when you know about the number of iterations.
8+
Example, print a number 10 times.
9+
10+
"""
11+
12+
n = 5
13+
while n < 5:
14+
print(n)
15+
n += 1
16+
17+
"""
18+
We use range function for iteration in for loop.
19+
Some facts about range function:
20+
1. By default, it starts with 0.
21+
2. range(stop_value) equals to i < stop_value. It means last digit is always exclusive.
22+
3. You can also specify the starting value like this => range(start, stop).
23+
4. By default, it increments by +1.
24+
5. You can also specify the incrementing value like this => range(start, stop, increment).
25+
"""
26+
27+
# Looping from i = 0 to i = 4
28+
for i in range(5):
29+
print(i)
30+
31+
# Looping from i = 2 to i = 5
32+
for i in range(2, 6):
33+
print(i)
34+
35+
# Looping from i = 5 to i = 2 with -1 increment
36+
for i in range(5, 1, -1):
37+
print(i)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import math
2+
3+
# Division is decimal by default
4+
print('5 / 2 = ', 5 / 2)
5+
# output => 2.5
6+
7+
# Double slash rounds down to integer
8+
print('5 // 2 = ', 5 // 2)
9+
# output => 2
10+
11+
# CAREFUL: most languages round towards 0 by default
12+
# python rounds towards minimum value
13+
# So negative numbers will round down
14+
print('-3 // 2 = ', -3 // 2)
15+
# output => -2 but it should be -1
16+
17+
# A workaround for rounding towards zero
18+
# is to use decimal division and then convert to int.
19+
print('-3 // 2 = ', int(-3 / 2))
20+
# output => -1
21+
22+
# Modding is similar to most languages
23+
print('10 % 3 = ', 10 % 3)
24+
# output => 1
25+
26+
# Except for negative values
27+
print('-10 % 3 = ', -10 % 3)
28+
29+
# To be consistent with other languages modulo use math fmod
30+
31+
print(math.fmod(-10, 3))
32+
33+
# More math helpers
34+
print(math.floor(3 / 2)) # rounds down
35+
print(math.ceil(3 / 2)) # rounds up
36+
print(math.sqrt(2))
37+
print(math.pow(2, 3))
38+
39+
# Max / Min Int
40+
float("inf")
41+
float("-inf")
42+
43+
# Python numbers are infinite so they never overflow
44+
print(math.pow(2, 200))
45+
46+
# But still less than infinity
47+
print(math.pow(2, 200) < float("inf"))
48+
# output => True

python_for_coding_interviews/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)