Python Codes
Kanalga Telegram’da o‘tish
This channel will serve you all the codes and programs which are related to Python. We post the codes from the beginner level to advanced level.
Ko'proq ko'rsatish7 090
Obunachilar
Ma'lumot yo'q24 soatlar
Ma'lumot yo'q7 kunlar
Ma'lumot yo'q30 kunlar
Postlar arxiv
7 090
Time complexity in above Picture
Factorial of a number using Dynamic Programming:
CODE:
calculated = {}
def fib(n):
if n == 0: # base case 1
return 0
if n == 1: # base case 2
return 1
elif n in calculated:
return calculated[n]
else: # recursive step
calculated[n] = fib(n-1) + fib(n-2)
return calculated[n]
Share and Support
@Python_Codes7 090
Time complexity in above Picture
Factorial using Recursion
CODE:
def fib(n):
if n <= 0: # base case 1
return 0
if n <= 1: # base case 2
return 1
else: # recursive step
return fib(n-1) + fib(n-2)
Share and Support
@Python_Codes7 090
Dynamic Programming:
👉 In simple words, the concept behind dynamic programming is to break the problems into sub-problems and save the result for the future so that we will not have to compute that same problem again.
👉 Dynamic programming is a problem-solving technique for resolving complex problems by recursively breaking them up into sub-problems, which are then each solved individually. Dynamic programming optimizes recursive programming and saves us the time of re-computing inputs later.
Share and Support
@Python_Codes
7 090
#numpy
NumPy
Smart use of ‘:’ to extract the right shape
Sometimes you encounter a 3-dim array that is of shape (N, T, D), while your function requires a shape of (N, D). At a time like this, reshape() will do more harm than good, so you are left with one simple solution:
Example:
for t in xrange(T):
x[:, t, :] = # ...
Share and Support
@Python_Codes7 090
#numpy
NumPy
Broadcasting
Broadcasting describes how NumPy automatically brings two arrays with different shapes to a compatible shape during arithmetic operations. Generally, the smaller array is “repeated” multiple times until both arrays have the same shape. Broadcasting is memory-efficient as it doesn’t actually copy the smaller array multiple times.
Code:
import numpy as np A = np.array([1, 2, 3]) res = A * 3 # scalar is broadcasted to [3 3 3] print(res)Output: # [3 6 9] Share and Support @Python_Codes
7 090
#Basics
Convert a value into a complex number
print(complex(10, 2))Output: (10+2j) Share and Support @Python_Codes
7 090
#Basics
Condition inside the print function
def is_positive(number):
print("Positive" if number > 0 else "Negative")
is_positive(-3)
Output:
Negative
Share and Support
@Python_Codes7 090
🕹 Want to work in the Gaming industry?
We collected jobs for python 🐍 developers in the Gaming industry in India 🇮🇳
Click the link below to see them in our telegram channel.
👉 Click here: https://t.me/+ORGwFAgg2HliZDNi
7 090
#Basics
Swap keys and values of a dictionary
dictionary =
{"a": 1, "b": 2, "c": 3}
reversed_dictionary = {j: i for i, j in dictionary.items()}
print(reversed)
Output:
{1: 'a', 2: 'b', 3: 'c'}
Share and Support
@Python_Codes7 090
from turtle import *
color('red', 'green')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
@python_codes
7 090
#Basics
Checking if two words are anagrams
Code:
from collections import Counter
def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)
# or without having to import anything
def is_anagram(str1, str2):
return sorted(str1) == sorted(str2)
print(is_anagram('code', 'doce'))
print(is_anagram('python', 'yton'))
Output:
True
False
Share and Support
@Python_Codes7 090
#Basics
Check The Memory Usage Of An Object.
Code:
import sys x = 1 print(sys.getsizeof(x))Output: 28 Share and Support @Python_Codes
7 090
#Basics
Find The Most Frequent Value In A List
Code:
test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] print(max(set(test), key = test.count))Output: 4 Share and Support @Python_Codes
7 090
#Basics
Return Multiple Values From Functions.
Code:
def x():
return 1, 2, 3, 4
a, b, c, d = x()
Input:
print(a, b, c, d)
Output:
1 2 3 4
Share and Support
@Python_Codes7 090
From Today we start from basic useful Python codes which are useful to everyone while coding in python
7 090
curry
Curries a function.
Use functools.partial() to return a new partial object which behaves like fn with the given arguments, args, partially applied.
CODE:
from functools import partial
def curry(fn, *args):
return partial(fn, *args)
Examples:
add = lambda x, y: x + y
add10 = curry(add, 10)
add10(20) # 30
Share and Support
@Python_Codes