cheat sheet Python module 1
cheat sheet Python module 1
# This is a comment
Comments are lines of text that are ignored by the Python interpreter when
Comments
executing the code<./td>
Syntax:
concatenated_string = string1 + string2
Example:
x=7
# Integer Value
y=12.4
# Float Value
is_valid = True
# Boolean Value
is_valid = False
# Boolean Value
Data Types - Integer - Float - Boolean - String F_Name = "John"
# String Value
Example:
my_string="Hello"
char = my_string[0]
Example:
my_string="Hello"
length = len(my_string)
about:blank 1/3
31/03/2025, 15:30 about:blank
Example:
my_string="Hello"
uppercase_text = my_string.lower()
Example:
print("Hello, world")
print(a+b)
Example:
x = 9 y = 4
result_add= x + y # Addition
result_sub= x - y # Subtraction
- Addition (+): Adds two values together. result_mul= x * y # Multiplication
result_div= x / y # Division
- Subtraction (-): Subtracts one value from another. result_fdiv= x // y # Floor Division
- Multiplication (*): Multiplies two values. result_mod= x % y # Modulo</td>
Python Operators - Division (/): Divides one value by another, returns a float.
- Floor Division (//): Divides one value by another, returns the quotient as
an integer.
- Modulo (%): Returns the remainder after division.
Example:
my_string="Hello"
new_text = my_string.replace("Hello", "Hi")
Syntax:
substring = string_name[start:end]
about:blank 2/3
31/03/2025, 15:30 about:blank
Example:
my_string="Hello"
split_text = my_string.split(",")
Example:
my_string="Hello"
trimmed = my_string.strip()
Example:
my_string="Hello"
uppercase_text = my_string.upper()
Syntax:
variable_name = value
Variable
Assigns a value to a variable. Example:
Assignment
name="John" # assigning John to variable name
x = 5 # assigning 5 to variable x
about:blank 3/3