Python for Programmers: Fast Track to Productivity
📅 02/10/2025
If you’re an experienced programmer looking to add Python to your toolkit, this guide is for you(writen for my wife who is a JS dev🤫😅). We’re skipping the “what is a variable” explanations and focusing on what makes Python different and powerful.
This comprehensive guide covers the essentials you need to be productive in Python: from basic data types and control flow, through lists and dictionaries, to functions, classes, and exception handling. We’ll also touch on Python-specific features like comprehensions and special methods that make the language elegant and expressive.
Let’s get started. To start execute in collab execeute a cell by using key (shift and enter or return).
Primitive Data Types
int: Whole numbers (e.g., 10, -5, 0)
float: Decimal numbers (e.g., 10.0, 3.14, -2.5)
str: Text/strings (e.g., “hello”, ‘world’)
bool: True/False values
NoneType: Python’s null value (None)
Using function type, we can fetch the type of a variable.
noi =10nof =10.1s ="i am string"bool_d =Truenone_d =Nonetype(noi), type(nof), type(s), type(bool_d), type(none_d)
(int, float, str, bool, NoneType)
Control flows
Indentation is mandatory - Python uses whitespace to define code blocks, not just for readability
Consistent indentation - All lines at the same block level must have the same indentation
Standard is 4 spaces (though tabs work, mixing them causes errors)
Colon usage - Control structures end with a colon : before the indented block
Conditional statements
age =18if age >=18:print("Adult")elif age >=13:print("Teenager")else:print("Child")
Adult
Loops
# For loopfor i inrange(3):print(f"For: {i=}")# While loopcount =0while count <3:print(f"While: {count=}") count +=1
List: Mutable, ordered collection - Can contain different data types - Supports indexing and slicing - Dynamic sizing (can grow/shrink) - use list keyword for init
Dictionary: Mutable, unordered key-value mapping - Keys must be immutable and unique - Fast key-based lookup - Dynamic sizing - use dict keyword for init
List
lis =list([1, 2, 3, 4])print(f"{lis=}")print(f"lenght of the lis : {len(lis)}")# indexing starting for 0print(f"{lis[0]=}")
lis=[1, 2, 3, 4]
lenght of the lis : 4
lis[0]=1
# adding new element to the listlis.append(5)print(f"{lis=}")
lis=[1, 2, 3, 4, 5]
lis2 = [6, 7]lis1 = lis + lis2print(f"{lis1=}")
lis1=[1, 2, 3, 4, 5, 6, 7]
# Get a portion of the listprint(f"{lis1[1:4]=}") # elements from index 1 to 3
lis1[1:4]=[2, 3, 4]
print(f"{lis=}")lis.insert(0, 99) # inserting value at the perticular indexprint(f"{lis=}")
lis=[1, 2, 3, 4, 5]
lis=[99, 1, 2, 3, 4, 5]
print(f"{lis=}")print(lis.pop()) # remove last elementprint(f"{lis=}")
lis=[99, 1, 2, 3, 4, 5]
5
lis=[99, 1, 2, 3, 4]
print(f"{lis=}")print(lis.remove(2)) # remove the 2nd elementprint(f"{lis=}")
lis=[99, 1, 2, 3, 4]
None
lis=[99, 1, 3, 4]
Dictionary
dic =dict({'a':1, 'b':2})dic
{'a': 1, 'b': 2}
dic =dict({'a':1, 'b':2}) # can be written as like {'a':1, 'b':2} without dictprint(f"{dic=}")print(f"{dic['a']=}") # indexing the dictionary with a key value
dic={'a': 1, 'b': 2}
dic['a']=1
# adding new entry to the dictionaydic['c'] =3print(f"{dic=}")
iterating wrt both key and values without explicitly indexing
for k, v in dic.items():print(f"{k=} -> {v=}")
k='a' -> v=1
k='b' -> v=2
#Safe key access uncommnet the below line and run#print(dic['c'])dic.get('item', "does not exists")
'does not exists'
# update a perticular value of a given keydic['a'] =1000print(f"{dic=}")
dic={'a': 1000, 'b': 2}
There are couple of other Data struct below are those: - Tuple: immutable list,ref - Set: As name suggest it will store object, ref
List and Dictionary comprehension
It is more consise way to build list and dict with explicitly using those key words. First I will create a list from 0 to 10, then filter out only the positive number.
lis =list(range(10))lis
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd = [i for i in lis if i %2]even = [i for i in lis ifnot i %2 ]even, odd
([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
dic = {chr(65+ i): i for i inrange(5)}dic
{'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4}
Function
starts with key word def
it should have a have and a list of args
we can add in types in the function definations, it will be used for type hinting. The compile don’t enforce types at runtime. The function accepts any types that support the + operator, even though we hinted int
def func():print("Hello world")func()
Hello world
def add(a:int, b:int=0):return a+bprint(add(10, 1))print(add(10)) # here b is defults to 0print(add(10.5, 0))print(add('10.5', '0'))
11
10
10.5
10.50
A gotcha By defult the function returns None. One should be keep those in mind.
Classes also suppourt inheritace, a base example can be found here
class L:def__init__(self, lis):print(f"init is called")self.lis = lisdef__str__(self):# it return length of the lis along with first 5 elementreturnf"{len(self.lis)}{self.lis[:5]}"def__len__(self):returnlen(self.lis)li = L(list(range(10)))print(li)print(f"{len(li)=}")
init is called
10 [0, 1, 2, 3, 4]
len(li)=10
Monkey patching is dynamically modifying a class or module at runtime by adding, replacing, or modifying its attributes or methods. While powerful, it should be used cautiously as it can make code harder to understand and maintain.
defiter(self):for i inself.lis:yield i# monkey patching L.__iter__=iterfor i in li:print(i, end=" ")
Purpose: Gracefully handle errors instead of crashing the program
try block: Contains code that might raise an exception
except block: Catches and handles specific exceptions
Multiple except blocks: Can catch different exception types separately
Exception as e: Captures the exception object for inspection
finally block (optional): Always executes, regardless of exceptions (useful for cleanup like closing files)
Raising exceptions: Use raise to throw exceptions intentionally
Common built-in exceptions: ValueError, TypeError, KeyError, IndexError, FileNotFoundError, ZeroDivisionError
try: result =10/0# This will raise ZeroDivisionErrorprint(f"{result=}")exceptZeroDivisionError:print("Cannot divide by zero!")exceptExceptionas e:print(f"An error occurred: {e}")finally:print("This always runs, error or not")
Cannot divide by zero!
This always runs, error or not
Conclusion
We have covered some basic part of Python programming language
The language is vast there is many other stuff are not covered below are few other imp for reference
File I/O : Reading and writing files (especially the with statement for context managers)