Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Fundamentals CheatSheet Updated

This cheat sheet covers fundamental Python programming concepts including printing, input, variables, data types, operators, conditionals, loops, functions, strings, and lists. It provides examples of basic operations, list manipulation, and introduces NumPy arrays. The document serves as a quick reference for essential Python syntax and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Fundamentals CheatSheet Updated

This cheat sheet covers fundamental Python programming concepts including printing, input, variables, data types, operators, conditionals, loops, functions, strings, and lists. It provides examples of basic operations, list manipulation, and introduces NumPy arrays. The document serves as a quick reference for essential Python syntax and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Programming Fundamentals – Final Exam Cheat Sheet

1. Print & Input


print("Hello, Python!") # Output
name = input("Enter your name: ") # Input (string)
print("Welcome,", name)

2. Variables & Data Types


x = 5 # int
pi = 3.14 # float
name = "Ali" # str
flag = True # bool
print(type(name)) # <class 'str'>

3. Arithmetic, Comparison & Logical Operators


a, b = 10, 3
print(a + b, a - b, a * b, a / b, a // b, a % b, a ** b)
print(a > b, a == b, a != b) # Comparison
print(a > 5 and b < 5) # Logical AND

4. Type Casting
x = int("5") # 5
y = float("3.5") # 3.5
z = str(123) # "123"
print(str(x) + " apples") # "5 apples"

5. Conditionals (if - elif - else)


marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
else:
print("Below average")

6. Loops (for, while, break, continue)


for i in range(1, 6): # 1 to 5
if i == 4: continue
print(i)

i = 0
while i < 3:
print(i)
i += 1
7. Functions (with default & return)
def greet(name="Guest"):
print("Hello", name)

def add(a, b):


return a + b

greet("Ali") # Hello Ali


print(add(5, 3)) # 8

8. Strings & String Methods


s = " Hello World "
print(s.strip()) # 'Hello World'
print(s.upper()) # ' HELLO WORLD '
print("Hello" in s) # True
print(s.replace("World", "Python")) # ' Hello Python

9. Lists (Create, Access, Modify, Loop)


fruits = ["apple", "banana", "cherry"]
fruits.append("mango") # Add
fruits[1] = "orange" # Modify
del fruits[0] # Delete
print(fruits) # ['orange', 'cherry', 'mango']

for fruit in fruits:


print(fruit)

print(fruits[::-1]) # Reverse: ['mango', 'cherry', 'orange']

10. List Methods


nums = [1, 2, 2, 3]
print(len(nums), sum(nums)) # 4 8
print(nums.count(2)) # 2
nums.remove(1) # Removes value 1
nums.sort()
print(nums) # [2, 2, 3]

11. List Slicing & Nested Lists


numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]

matrix = [
[1, 2],
[3, 4]
]
print(matrix[1][0]) # 3
12. List Copying vs Referencing
a = [1, 2]
b = a
b.append(3)
print(a) # [1, 2, 3] – Same object

c = a[:] # Copy
c.append(4)
print(a) # [1, 2, 3]
print(c) # [1, 2, 3, 4]

13. List Comprehension


squares = [x**2 for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]

14. Swapping Values


x, y = 5, 10
x, y = y, x
print(x, y) # 10 5

15. NumPy Arrays (Array Concepts)


import numpy as np

arr = np.array([1, 2, 3])


print(arr[0]) # 1
print(arr + 5) # [6 7 8]

mat = np.array([[1, 2], [3, 4]])


print(mat[0][1]) # 2
print(mat.shape) # (2, 2)

16. List vs Array Trick


list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(list1 + list2) # [1, 2, 3, 4, 5, 6]

a1 = np.array(list1)
a2 = np.array(list2)
print(a1 + a2) # [5 7 9]

17. Dry Run Example


x = 3
y = 5
if x < y:
x += 1
print(x) # Output: 4
18. Tricky List Operation Example
nums = [1, 2, 3]
nums2 = nums
nums2[0] = 99
print(nums) # [99, 2, 3]

nums3 = nums.copy()
nums3[1] = 77
print(nums) # [99, 2, 3]
print(nums3) # [99, 77, 3]

19. Nested Loops


# 3x3 star grid
for row in range(3):
for col in range(3):
print("*", end=" ")
print()

# Loop through 2D list


matrix = [
[1, 2],
[3, 4],
[5, 6]
]
for row in matrix:
for value in row:
print(value, end=" ")
print()

20. Manipulating Lists – Add, Modify, Remove, Extend


# Start with a basic list
my_list = ['a', 'b', 'c']
print("Original:", my_list)

# Add items
my_list.append('d')
my_list.insert(1, 'x')
my_list.extend(['e', 'f'])
print("After adding:", my_list)

# Modify items
my_list[2] = 'B'
my_list[0:2] = ['Z', 'Y'] # slice replacement
print("After modifying:", my_list)

# Remove items
my_list.pop(3) # removes by index
my_list.remove('Y') # removes by value
del my_list[0] # deletes by index
print("After removing:", my_list)

# + operator creates a new list


combined = [1, 2] + [3, 4]
print("Combined with +:", combined)

# extend vs append difference


a = [1, 2]
b = [3, 4]
a.append(b)
print("Append list b:", a) # [1, 2, [3, 4]]
a = [1, 2]
a.extend(b)
print("Extend with b:", a) # [1, 2, 3, 4]

# extend with a string


chars = ['a', 'b']
chars.extend("xyz")
print("Extend with string:", chars)

21. Replace List Elements


fruits = ["apple", "banana", "cherry", "date"]
fruits[1] = "grape"
fruits[2:4] = ["kiwi", "lemon"]
fruits[1:2] = ["orange", "peach", "mango"]
fruits[3:5] = []
print("Modified fruits:", fruits)

22. Delete List Elements


languages = ["Python", "Java", "C++", "JavaScript", "Java"]
languages.remove("Java") # Removes first 'Java'
removed = languages.pop(1) # Removes 'C++'
last = languages.pop() # Removes last
del languages[0] # Deletes 'Python'
print("Remaining languages:", languages)

nums = [10, 20, 30, 40, 50, 60]


del nums[1:4] # Deletes 20, 30, 40
print("Sliced nums:", nums)

You might also like