0% found this document useful (0 votes)
8 views10 pages

Python Programming Cheat Sheet

This document is a cheat sheet for Python programming fundamentals, covering various concepts such as logical operators, class definitions, function definitions, loops, conditionals, and exception handling. Each concept is explained with syntax and code examples to illustrate its usage. It serves as a quick reference guide for Python programming syntax and structure.

Uploaded by

hersmall.project
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)
8 views10 pages

Python Programming Cheat Sheet

This document is a cheat sheet for Python programming fundamentals, covering various concepts such as logical operators, class definitions, function definitions, loops, conditionals, and exception handling. Each concept is explained with syntax and code examples to illustrate its usage. It serves as a quick reference guide for Python programming syntax and structure.

Uploaded by

hersmall.project
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

5/28/25, 2:40 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
AND marks = 90
`True`. 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 Example:


Class Definition
their attributes 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 Syntax:


specific task or set of tasks when called.
def function_name(parameters): # Function body

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

about:blank 1/10
5/28/25, 2:40 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

Syntax:
for variable in sequence: # Code to repeat

Example 1:
for num in range(1, 10):
print(num)

A `for` loop repeatedly executes a block of code for a


For Loop specified number of iterations or over a sequence of
elements (list, range, string, etc.).

Example 2:
fruits = ["apple", "banana", "orange", "grape", "kiwi"]
for fruit in fruits:
print(fruit)

Function Call A function call is the act of executing the code within Syntax:
the function using the provided arguments.
function_name(arguments)

about:blank 2/10
5/28/25, 2:40 AM about:blank

Example:
greet("Alice")

Syntax:
variable1 >= variable2

Example 1:

5 >= 5 and 9 >= 5

Greater Than or Checks if the value of variable1 is greater than or equal


Equal To(>=) to variable2.

returns True

Example 2:
quantity = 105
minimum = 100
quantity >= minimum

returns True

Greater Than(>) Checks if the value of variable1 is greater than Syntax:


variable2.
variable1 > variable2

Example 1: 9 > 6

returns True

Example 2:
age = 20
max_age = 25
age > max_age

about:blank 3/10
5/28/25, 2:40 AM about:blank

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!")

Syntax:
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if no condition is True

Executes the first code block if condition1 is `True`,


If-Elif-Else otherwise checks condition2, and so on. If no condition Example:
is `True`, the else block is executed.
score = 85 # Example score
if score >= 90:
print("You got an A!")
elif score >= 80:
print("You got a B.")
else:
print("You need to work harder.")
# Output = You got a B.

If-Else Statement Executes the first code block if the condition is `True`, Syntax:
otherwise the second block.
if condition: # Code, if condition is True
else: # Code, if condition is False

Example:

about:blank 4/10
5/28/25, 2:40 AM about:blank
if age >= 18:
print("You're an adult.")
else:
print("You're not an adult yet.")

Syntax:
variable1 <= variable2

Example 1:

5 <= 5 and 3 <= 5

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

returns True

Example 2:

size = 38
max_size = 40
size <= max_size

returns True

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

Example 1:
4 < 6

returns True

Example 2:
score = 60

about:blank 5/10
5/28/25, 2:40 AM about:blank
passing_score = 65
score < passing_score

returns True

Syntax:
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)
`break` exits the loop prematurely. `continue` skips the
Loop Controls rest of the current iteration and moves to the next
iteration.

Example 2:
for num in range(1, 6):
if num == 3:
continue
print(num)

NOT Returns `True` if variable is `False`, and vice versa. Syntax:


not variable

Example:
isLocked = False
print(not isLocked)

about:blank 6/10
5/28/25, 2:40 AM about:blank
returns True if the variable is False (i.e., unlocked).

Syntax:
variable1 != variable2

Example:
a = 10
b = 20
a != b

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

returns True

Example 2:
count=0
count != 0

returns False

Syntax:
object_name = ClassName(arguments)

Creates an instance of a class (object) using the class


Object Creation
constructor. Example:
person1 = Person("Alice", 25)

OR Returns `True` if either statement1 or statement2 (or Syntax:


both) are `True`. Otherwise, returns `False`.
statement1 or statement2

Example:
"Farewell Party Invitation"

about:blank 7/10
5/28/25, 2:40 AM about:blank
Grade = 12 grade == 11 or grade == 12

returns True

Syntax:
range(stop)
range(start, stop)
range(start, stop, step)

Generates a sequence of numbers within a specified


range()
range. Example:
range(5) #generates a sequence of integers from 0 to 4.
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


Return Statement
function to its caller. Example:
def add(a, b): return a + b
result = add(3, 5)

Try-Except Block Tries to execute the code in the try block. If an Syntax:
exception of the specified type occurs, the code in the
try: # Code that might raise an exception except
except block is executed.
ExceptionType: # Code to handle the exception

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

about:blank 8/10
5/28/25, 2:40 AM about:blank

Syntax:
try: # Code that might raise an exception except
ExceptionType: # Code to handle the exception
else: # Code to execute if no exception occurs

Try-Except with Else Code in the `else` block is executed if no exception Example:
Block occurs in the try block.
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

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

While Loop A `while` loop repeatedly executes a block of code as Syntax:


long as a specified condition remains `True`.
while condition: # Code to repeat

Example:

about:blank 9/10
5/28/25, 2:40 AM about:blank
count = 0
while count < 5:
print(count)
count += 1

© IBM Corporation. All rights reserved.

about:blank 10/10

You might also like