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

CheatSheet PythonProgramming

This document is a cheat sheet for Python programming fundamentals, detailing various packages and methods along with their syntax and code examples. Key topics include logical operators (AND, OR, NOT), control flow statements (if, for loops), function definitions and calls, class definitions, and exception handling. It serves as a quick reference guide for essential Python programming concepts and syntax.

Uploaded by

chispitaboom53
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)
4 views10 pages

CheatSheet PythonProgramming

This document is a cheat sheet for Python programming fundamentals, detailing various packages and methods along with their syntax and code examples. Key topics include logical operators (AND, OR, NOT), control flow statements (if, for loops), function definitions and calls, class definitions, and exception handling. It serves as a quick reference guide for essential Python programming concepts and syntax.

Uploaded by

chispitaboom53
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
You are on page 1/ 10

6/8/25, 6:42 p.m.

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`.
AND marks = 90
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 Example:


Class Definition
attributes and behaviors.
class Person:
def __init__(self, name, age):
self.name = name
self.age = 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/10
6/8/25, 6:42 p.m. 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,
for variable in sequence: # Code to repeat
string, etc.).

Example 1:

for num in range(1, 10):


print(num)

Example 2:

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


for fruit in fruits:
print(fruit)

about:blank 2/10
6/8/25, 6:42 p.m. about:blank

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

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

about:blank 3/10
6/8/25, 6:42 p.m. about:blank

Example 1: 9 > 6

returns True

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
if condition1:
block is executed. # 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:
print("You got a B.")
else:
print("You need to work harder.")
# Output = You got a B.

about:blank 4/10
6/8/25, 6:42 p.m. about:blank

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


If-Else Statement Example:
the second block.
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 variable2.
To(<=)

returns True

Example 2:
size = 38
max_size = 40
size <= max_size

returns True

about:blank 5/10
6/8/25, 6:42 p.m. about:blank

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)

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

about:blank 6/10
6/8/25, 6:42 p.m. about:blank

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).

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

about:blank 7/10
6/8/25, 6:42 p.m. about:blank

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
`True`. Otherwise, returns `False`. grade = 12
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.


range(2, 10) #generates a sequence of integers from 2 to 9.
range(1, 11, 2) #generates odd integers from 1 to 9.

about:blank 8/10
6/8/25, 6:42 p.m. about:blank

Syntax:
return value

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


Return Statement Example:
to 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


Try-Except Block Example:
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

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

about:blank 9/10
6/8/25, 6:42 p.m. about:blank

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 of whether Example:
Finally Block an exception occurred.
try:
file = open("data.txt", "r")
data = file.read()
except FileNotFoundError:
print("File not found.")
finally:
file.close()

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

© IBM Corporation. All rights reserved.

about:blank 10/10

You might also like