Comparison Operators: Python Hello World! Variable Data Manipulation

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Comparison Operators Iterations (Loops)

PYTHON CHEATSHEET Greater than x>y


Loops allow you to repeat a code block multiple times.

FOR ABSOLUTE BEGINNERS


# Iterations (Loops) Example
Less than x<y # Using a for loop to print numbers from 1 to 1000
for i in range(1,1001):
Equal to x==y print(i)

Python Hello World! Not equal to x!=y


# Using a while loop to count down from 5 to 1
count = 5
while count > 0:
print ("Hello World!") # printing “Hello World” Greater than or equal to x>=y print(count)
count -= 1
Less than or equal to x<=y

Variable Logical Operators


A variable is a named container that holds data in a program. Logical AND x>4 and y>4 Data Manipulation
count = 10 # 'count' is a variable holding the value 10
x>4 or y>4
Logical
SubtractOR
and assign String
Logical
MultiplyNOT
and assign not x>4 A string is a sequence of characters enclosed in quotes.

Data Types
Initializing the string
Compound Assignment Operators Use single or double quotes to initialize a string.
Data types categorize data and specify the operations allowed on it.
Add and Assign x+=y text = "Hello, World!"
• int: Integer values (5, 0, -1, etc.)
• float: Floating-point values (2.33333, -2500.001, 20.0, etc.) Divide and assign x/=y
Accessing the characters in a string
• str: String values (educative, python, etc.) Integer divide and assign x//=y Use indexing to access individual characters.
first_char = text[0] # Accesses 'H'

Operators Sequential Execution


Slice the string
Slice a string to extract substrings.
Operators are symbols or keywords that perform operations on Statements are executed one after the other in the order
substring = text[7:12] # Extracts 'World'
they appear in the code.
values or variables.
# Sequential Execution Example
print("Step 1") List
Arithmetic Operators print("Step 2")
print("Step 3")
A list is an ordered collection of items.

Addition x+y Creating a list

Subtraction x-y Branching (Conditional Statements) my_list = [1, 2, 3, 'apple', 'banana']

Branching allows you to execute different code blocks


Multiplication x*y based on a condition. Accessing individual values
Division x/y first_item = my_list[0] # Accesses the first item
# Branching (Conditional Statements) Example
age = 25
Non-integer division x//y
if age >= 18: Slice the list
Modulo x%y print("You are an adult.")
else: sublist = my_list[1:4] # Extracts items at positions 1, 2, 3
print("You are not an adult.")
Power x**y
# Slicing a portion from the end
sublist = my_list[-3:] # Get the last three elements of my_list
Python’s Built-in Toolbox Errors
PYTHON CHEATSHEET Built-in Functions for Strings
Errors in programming are unexpected issues that disrupt the
proper execution of a program.

FOR ABSOLUTE BEGINNERS The is... methods


Syntax Errors
Mistakes in the structure or "grammar" of the code.
• isalpha(): Checks if all characters are alphabetic if x = 5: # Incorrect use of '=' instead of '=='

Introduction to Functions • isdigit(): Checks if all characters are digits


• isalnum(): Checks if all characters are alphanumeric
print("x is 5")

Runtime Errors
Function • islower(): Checks if all characters are lowercase
• isupper(): Checks if all characters are uppercase
Errors that occur during execution (e.g., dividing by zero)
A function is a reusable block of code that performs a specific task
a = 10
or a set of related tasks. b = 0
result = a / b # Division by zero
Syntax of a Function Methods related to case change
def function_name(parameters):
• lower(): Converts all characters to lowercase Logical Errors
# Function body • upper(): Converts all characters to uppercase The code runs without crashing but produces incorrect results.
# ...
return result • title(): Converts the first character of each word to side_length = 5
uppercase # Incorrect formula for calculating the area of a square (uses π
• def: Keyword to define a function r^2)
area = 3.14 * (side_length ** 2)
• function_name: Name of the function print("Area of the square:", area)
• parameters: Input values to the function Search-related methods
• find(): Returns the index of the first occurrence
• return: Keyword to return a result (optional)
• count(): Returns the number of occurrences
Note: A function body comprises a set of instructions that compute the result • startswith(): Checks if the string starts with a
based on the input parameters. Explore the Fundamentals of Python
specified prefix
programming with the best course for you!
• endswith(): Checks if the string ends with a specified
suffix
Function Calls
result = function_name(arguments)

• function_name: Name of the function Built-in Functions for Lists


• arguments: Values passed as function parameters
• result: Result returned by the function (if applicable) Value-inserting methods
• append(): Adds an element at the end
• insert(): Inserts an element at a specified position

Practice: Creating and Calling Functions Value-removing methods


• remove(): Removes the first occurrence of a value
def greet(name):
message = f"Hello, {name}!" • pop(): Removes and returns an element by index
return message
Search-related methods
message = greet("Alice") • index(): Returns the index of the first occurrence of
print(message) # Output: Hello, Alice! a value
• count(): Returns the number of occurrences
Arrangement-related methods
• sort(): Sorts the list in ascending order Learn To Code: Python For Absolute Beginners
• reverse(): Reverses the order of elements
www.educative.io

You might also like