Python Basics Notes for interns
Python Basics Notes for interns
This document covers fundamental Python concepts: variables, data types, operators, built-
in functions, conditional statements, loops, and functions, with simple examples for each.
1. Variables
Variables in Python are used to store data. Python is dynamically typed, meaning you don’t
need to explicitly declare the type of a variable.
Variables can be declared without keywords like var, let, or const (unlike JavaScript).
Variable names are case-sensitive and should follow naming conventions (e.g.,
lowercase with underscores for multi-word names).
Example:
name = "Alice"
age = 25
Python automatically determines the data type based on the assigned value.
Example:
x = 10 # x is an integer
print(x) # Output: 10
2. Data Types
Example:
num = 42 # int
Types:
Example:
3. Operators
Example:
a, b = 10, 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
Example:
x=5
x += 3 # x = x + 3
print(x) # Output: 8
x *= 2 # x = x * 2
print(x) # Output: 16
Example:
a, b = 5, 3
Example:
is_adult = True
has_ticket = False
Operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift).
Example:
a, b = 5, 3 # 5 = 0101, 3 = 0011
4. Built-in Functions/Methods
my_list = [1, 2, 3]
print(len(my_list)) # Output: 3
x = 42
num_str = "123"
num = int(num_str)
for i in range(3):
print(i) # Output: 0, 1, 2
numbers = [1, 2, 3, 4]
print(sum(numbers)) # Output: 10
print(max(numbers)) # Output: 4
print(min(numbers)) # Output: 1
print(index, value)
# Output:
# 0 apple
# 1 banana
print(abs(-5)) # Output: 5
x = 42
print(chr(65)) # Output: A
print(ord("A")) # Output: 65
# help() - Documentation
5. Conditional Statements
5.1 If Statement
Example:
age = 18
Example:
age = 16
else:
Example:
score = 85
print("Grade: A")
print("Grade: B")
print("Grade: C")
else:
print("Grade: D or F")
# Output: Grade: B
6. Loops
Example:
print(i)
# Output:
#1
#2
#3
#4
#5
Example:
count = 1
while count <= 5:
print(count)
count += 1
# Output:
#1
#2
#3
#4
#5
Example:
print(i, j)
# Output:
#11
#12
#21
#22
Example:
if i == 3:
# Output: 1, 2
if i == 3:
continue # Skip 3
print(i)
# Output: 1, 2, 4, 5
if i == 3:
pass # Placeholder
print(i)
# Output: 1, 2, 3, 4, 5
7. Functions
Example:
return a + b
result = add(3, 4)
print(result) # Output: 7
def greet(name):
print(f"Hello, {name}!")
Example:
square = lambda x: x * x
print(square(5)) # Output: 25
nums = [1, 2, 3]
Example:
def sum_numbers(*args):
return sum(args)
def print_info(**kwargs):
print(f"{key}: {value}")
print_info(name="Alice", age=25)
# Output:
# name: Alice
# age: 25