logo

Interview Simplified

Basics

Quick reference guide for Python basics.

🚌 Fundamentals

Input & Output

print("Hello, World!")
# Hello, world!

a = 1
print('Hello world!', a)
# Hello world! 1

name = input("Enter your name: ")
print(f"Hello, {name}!")
# Enter your name: Sid
# Hello, Sid

Variables & Data Types

# Numbers
age = 25                    # int
price = 19.99              # float
coordinate = 2 + 3j        # complex

# Text
name = "Alice"             # string

# Boolean
is_student = True
is_active = False

# None
result = None              # NoneType

Plus Equals

greeting = 'Hello'
greeting += ' world!'
greeting
# 'Hello world!'

number = 1
number += 1
number
# 2

my_list = ['item']
my_list *= 3
my_list
# ['item', 'item', 'item']

Conditionals

name = 'George'
if name == 'Debora':
    print('Hi Debora!')
elif name == 'George':
    print('Hi George!')
# Hi George!

x = 10
if x > 5:
    print("Greater")
elif x == 5:
    print("Equal")
else:
    print("Smaller")
# Greater

Switch-Case Statement

# Matching single values
# ----------------------------------
response_code = 201
match response_code:
    case 200:
        print("OK")
    case 201:
        print("Created")
    case 300:
        print("Multiple Choices")
# Created

# Default value
# ----------------------------------
response_code = 800
match response_code:
    case 200 | 201:
        print("OK")
    case 500 | 502:
        print("Internal Server Error")
    case _:
        print("Invalid Code")
# Invalid Code

Loops

# The for loop iterates over a list, tuple, dictionary, set or string:

pets = ['Bella', 'Milo', 'Loki']
for pet in pets:
    print(pet)
# Bella
# Milo
# Loki

for i in range(3):
    print(i)
# 0
# 1
# 2

for i in range(3, -1, -1):
    print(i)
# 3
# 2
# 1
# 0

x = 3
while x > 0:
    print(x)
    x -= 1
# 3
# 2
# 1

for i, item in enumerate([1, 2, 3]):
    print(f"Index: {i}, Item: {item}")
# Index: 0, Item: 1
# Index: 1, Item: 2
# Index: 2, Item: 3

Continue and Break

while True:
    name = input('Who are you? ')
    if name != 'Joe':
        continue
    password = input('Password? (It is a fish.): ')
    if password == 'swordfish':
        break
    print('Access granted.')

# Who are you? Charles
# Who are you? Debora
# Who are you? Joe
# Password? (It is a fish.): swordfish
# Access granted.

Strings

# String basics
name = "Python"
greeting = 'Hello there!'
multiline = """This is a
multiline string"""

# String concatenation
full_name = "John" + " " + "Doe"
print(full_name)  # John Doe

# String length
text = "Hello"
print(len(text))  # 5

# String indexing and slicing
word = "Python"
print(word[0])     # P (first character)
print(word[-1])    # n (last character)
print(word[0:2])   # Py (slicing from 0 to 2-1)
print(word[2:])    # thon (slicing from 2 to end)

# Looping through strings
for char in "Code":
    print(char)
# C
# o
# d
# e

# String methods
sentence = "python programming"
print(sentence.upper())        # PYTHON PROGRAMMING
print(sentence.capitalize())   # Python programming
print(sentence.count('p'))     # 2
print(sentence.replace('p', 'j')) # jython jrogramming

🚀 Functions

Function Arguments

def say_hello(name):
   print(f'Hello {name}')

say_hello('Carlos')
# Hello Carlos

say_hello('Wanda')
# Hello Wanda

Keyword Arguments

def say_hi(name, greeting):
   print(f"{greeting} {name}")

# without keyword arguments
say_hi('John', 'Hello')
# Hello John

# with keyword arguments
say_hi(name='Anna', greeting='Hi')
# Hi Anna

Return Values

def sum_two_numbers(number_1, number_2):
   return number_1 + number_2
result = sum_two_numbers(7, 8)
print(result)
# 15

Lambda Functions

def add(x, y):
    return x + y
add(5, 3)
# 8

#Is equivalent to the lambda function:
add = lambda x, y: x + y
add(5, 3)
# 8