0% found this document useful (0 votes)
5 views11 pages

Python Programming Basics Cheat Sheet

The document is a cheat sheet for Python programming fundamentals, covering various concepts such as logical operators, class definitions, function creation, control flow statements, and exception handling. It provides syntax and code examples for each concept, including 'if' statements, loops, and error handling techniques. This resource serves as a quick reference for essential Python programming syntax and functionality.

Uploaded by

carolrayen48
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Python Programming Basics Cheat Sheet

The document is a cheat sheet for Python programming fundamentals, covering various concepts such as logical operators, class definitions, function creation, control flow statements, and exception handling. It provides syntax and code examples for each concept, including 'if' statements, loops, and error handling techniques. This resource serves as a quick reference for essential Python programming syntax and functionality.

Uploaded by

carolrayen48
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/19/25, 3:09 AM about:blank

Python Programming Fundamentals Cheat Sheet


Package/Method Description Syntax and Code Example

Syntax:

statement1 and statement2

Example:
Returns `True` if both statement1 and statement2 are `True`. marks = 90
AND
Otherwise, returns `False`. attendance_percentage = 87
if marks >= 80 and attendance_percentage >= 85:
print("qualify for honors")
else:
print("Not qualified for honors")
# Output = qualify for honors

Syntax:

class ClassName: # Class attributes and methods

Defines a blueprint for creating objects and defining their attributes Example:
Class Definition
and behaviors.
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age

Define Function A `function` is a reusable block of code that performs a specific Syntax:
task or set of tasks when called.
def function_name(parameters): # Function body

Example:

def greet(name): print("Hello,", name)

about:blank 1/11
10/19/25, 3:09 AM about:blank

Syntax:

variable1 == variable2

Example 1:

5 == 5

Equal(==) Checks if two values are equal.

returns True

Example 2:

age = 25
age == 30

returns False

For Loop A `for` loop repeatedly executes a block of code for a specified Syntax:
number of iterations or over a sequence of elements (list, range,
string, etc.). for variable in sequence: # Code to repeat

Example 1:

for num in range(1, 10):


print(num)

Example 2:

fruits = ["apple", "banana", "orange", "grape", "kiwi"]

about:blank 2/11
10/19/25, 3:09 AM about:blank
for fruit in fruits:
print(fruit)

Syntax:

function_name(arguments)

A function call is the act of executing the code within the function
Function Call
using the provided arguments. Example:

greet("Alice")

Syntax:

variable1 >= variable2

Example 1:

5 >= 5 and 9 >= 5

Greater Than or Equal Checks if the value of variable1 is greater than or equal to
To(>=) variable2.

returns True

Example 2:

quantity = 105
minimum = 100
quantity >= minimum

returns True

about:blank 3/11
10/19/25, 3:09 AM about:blank

Syntax:

variable1 > variable2

Example 1: 9 > 6

returns True
Greater Than(>) Checks if the value of variable1 is greater than variable2.
Example 2:

age = 20
max_age = 25
age > max_age

returns False

Syntax:

if condition: #code block for if statement

If Statement Executes code block `if` the condition is `True`.


Example:

if temperature > 30:


print("It's a hot day!")

If-Elif-Else Executes the first code block if condition1 is `True`, otherwise Syntax:
checks condition2, and so on. If no condition is `True`, the else
block is executed. if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if no condition is True

Example:

score = 85 # Example score


if score >= 90:
print("You got an A!")
elif score >= 80:

about:blank 4/11
10/19/25, 3:09 AM about:blank
print("You got a B.")
else:
print("You need to work harder.")
# Output = You got a B.

Syntax:

if condition: # Code, if condition is True


else: # Code, if condition is False

Executes the first code block if the condition is `True`, otherwise Example:
If-Else Statement
the second block.
if age >= 18:
print("You're an adult.")
else:
print("You're not an adult yet.")

Less Than or Equal Checks if the value of variable1 is less than or equal to variable2. Syntax:
To(<=)
variable1 <= variable2

Example 1:

5 <= 5 and 3 <= 5

returns True

Example 2:

size = 38
max_size = 40
size <= max_size

about:blank 5/11
10/19/25, 3:09 AM about:blank

returns True

Syntax:

variable1 < variable2

Example 1:

4 < 6

Less Than(<) Checks if the value of variable1 is less than variable2.

returns True

Example 2:

score = 60
passing_score = 65
score < passing_score

returns True

Loop Controls `break` exits the loop prematurely. `continue` skips the rest of the Syntax:
current iteration and moves to the next iteration.
for: # Code to repeat
if # boolean statement
break
for: # Code to repeat
if # boolean statement
continue

Example 1:

for num in range(1, 6):


if num == 3:
break
print(num)

about:blank 6/11
10/19/25, 3:09 AM about:blank
Example 2:

for num in range(1, 6):


if num == 3:
continue
print(num)

Syntax:

not variable

Example:
NOT Returns `True` if variable is `False`, and vice versa.
isLocked = False
print(not isLocked)

returns True if the variable is False (i.e., unlocked).

Not Equal(!=) Checks if two values are not equal. Syntax:

variable1 != variable2

Example:

a = 10
b = 20
a != b

returns True

Example 2:

count=0
count != 0

about:blank 7/11
10/19/25, 3:09 AM about:blank

returns False

Syntax:

object_name = ClassName(arguments)

Object Creation Creates an instance of a class (object) using the class constructor.
Example:

person1 = Person("Alice", 25)

Syntax:

statement1 or statement2

Example:

Returns `True` if either statement1 or statement2 (or both) are "Farewell Party Invitation"
OR grade = 12
`True`. Otherwise, returns `False`.
if grade == 11 or grade == 12:
print("Farewell Party Invitation")
else:
print("Not eligible")

returns True

range() Generates a sequence of numbers within a specified range. Syntax:

range(stop)
range(start, stop)
range(start, stop, step)

Example:

range(5) #generates a sequence of integers from 0 to 4.

about:blank 8/11
10/19/25, 3:09 AM about:blank
range(2, 10) #generates a sequence of integers from 2 to 9.
range(1, 11, 2) #generates odd integers from 1 to 9.

Syntax:

return value

`Return` is a keyword used to send a value back from a function to


Return Statement Example:
its caller.

def add(a, b): return a + b


result = add(3, 5)

Syntax:

try: # Code that might raise an exception except


ExceptionType: # Code to handle the exception

Tries to execute the code in the try block. If an exception of the Example:
Try-Except Block
specified type occurs, the code in the except block is executed.
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input. Please enter a valid number.")

Try-Except with Else Code in the `else` block is executed if no exception occurs in the Syntax:
Block try block.
try: # Code that might raise an exception except
ExceptionType: # Code to handle the exception
else: # Code to execute if no exception occurs

about:blank 9/11
10/19/25, 3:09 AM about:blank
Example:

try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input. Please enter a valid number")
else:
print("You entered:", num)

Syntax:

try: # Code that might raise an exception except


ExceptionType: # Code to handle the exception
finally: # Code that always executes

Example:
Try-Except with Code in the `finally` block always executes, regardless of whether
Finally Block an exception occurred. try:
file = open("[Link]", "r")
data = [Link]()
except FileNotFoundError:
print("File not found.")
finally:
[Link]()

Syntax:

while condition: # Code to repeat

A `while` loop repeatedly executes a block of code as long as a Example:


While Loop
specified condition remains `True`.
count = 0
while count < 5:
print(count)
count += 1

about:blank 10/11
10/19/25, 3:09 AM about:blank

© IBM Corporation. All rights reserved.

about:blank 11/11

You might also like