Python Fundamentals
Python Fundamentals
1. Introduction
2.1 Variables
Variables are containers for storing data values. In Python, variables are created when
you assign a value to them.
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 1 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
3. Basic Operations
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 2 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
In [39]: a = 10
b = 3
print(a + b) # Addition: 13
print(a - b) # Subtraction: 7
print(a * b) # Multiplication: 30
print(a / b) # Division: 3.3333...
print(a % b) # Modulus: 1
print(a ** b) # Exponentiation: 1000
13
7
30
3.3333333333333335
1
1000
In [40]: x = 5
x += 3 # Equivalent to x = x + 3
x -= 2 # Equivalent to x = x - 2
x *= 4 # Equivalent to x = x * 4
x /= 2 # Equivalent to x = x / 2
In [41]: a = 5
b = 10
False
True
False
True
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 3 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
False
True
False
4. Control Flow
In [43]: age = 18
# Using range()
for i in range(5):
print(i)
apple
banana
cherry
0
1
2
3
4
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 4 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
In [45]: count = 0
0
1
2
3
4
# Continue statement
for i in range(10):
if i == 5:
continue
print(i)
0
1
2
3
4
0
1
2
3
4
6
7
8
9
5. Data Structures
Python provides various data structures to store collections of data.
5.1 Lists
Lists are used to store multiple items in a single variable.
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 5 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
# Accessing elements
print(fruits[0]) # Output: apple
# Adding elements
fruits.append('orange')
# Removing elements
fruits.remove('banana')
# List comprehension
squares = [x**2 for x in range(10)]
apple
5.2 Dictionaries
# Accessing values
print(person['name']) # Output: Ashish
Ashish
5.3 Tuples
# Accessing elements
print(coordinates[0]) # Output: 10
10
5.4 Sets
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 6 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
# Adding elements
unique_numbers.add(6)
# Removing elements
unique_numbers.remove(3)
6. Functions
Functions in Python are defined using the def keyword. They allow you to encapsulate
code for reuse and better organization.
# Calling a function
print(greet('Ashish')) # Output: Hello, Ashish!
Hello, Ashish!
6.3 Decorators
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 7 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
@debug
def multiply(a, b):
return a * b
7. File Handling
Python provides built-in functions to read from and write to files. This is essential for data
persistence and manipulation.
Hello, world!
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 8 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
8. Error Handling
Python uses try-except blocks to handle exceptions and errors gracefully. This ensures
your program can handle unexpected situations without crashing.
In [57]: try:
x = 10 / 0
except ZeroDivisionError:
print('Cannot divide by zero!')
try:
raise CustomError('This is a custom error')
except CustomError as e:
print(e) # Output: This is a custom error
4.0
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 9 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
# Creating an array
arr = np.array([1, 2, 3, 4])
print(arr)
[1 2 3 4]
[6 7 8 9]
def bark(self):
return 'Woof!'
# Creating an object
my_dog = Dog('Buddy', 3)
print(my_dog.name) # Output: Buddy
print(my_dog.bark()) # Output: Woof!
Buddy
Woof!
10.2 Methods
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 10 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
def bark(self):
return 'Woof!'
def get_age(self):
return self.age
10.3 Inheritance
def make_sound(self):
return 'Some sound'
class Dog(Animal):
def bark(self):
return 'Woof!'
Buddy
Some sound
Woof!
10.4 Polymorphism
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 11 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
class Dog:
def speak(self):
return 'Woof'
def make_animal_speak(animal):
print(animal.speak())
my_cat = Cat()
my_dog = Dog()
make_animal_speak(my_cat) # Output: Meow
make_animal_speak(my_dog) # Output: Woof
Meow
Woof
11.2 Generators
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 12 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
0
1
2
3
4
Enter a number: 5
The factorial of 5 is 120
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 13 of 14
01. Python Fundamentals - Jupyter Notebook 22/05/24, 5:53 PM
http://localhost:8888/notebooks/Downloads/LinkedIn/CheatSheets/01.%20Python%20Fundamentals.ipynb# Page 14 of 14