Play
00:00
00:00
Mute
Settings
Module 1 Cheat Sheet: Python Basics
Package/Method Description Code Example
1. 1
Comments Comments are lines of text that are ignored by the Python interpreter when executing the code<./td>
1. # This is a comment
Copied!Wrap Toggled!
Syntax:
1. 1
1. concatenated_string = string1 + string2
Copied!Wrap Toggled!
Concatenation Combines (concatenates) strings. Example:
1. 1
1. result = "Hello" + " John"</td>
Copied!Wrap Toggled!
Data Types - Integer - Float - Boolean - String Example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
1. x=7
2. # Integer Value
3. y=12.4
4. # Float Value
5. is_valid = True
6. # Boolean Value
7. is_valid = False
8. # Boolean Value
9. F_Name = "John"
10. # String Value
Copied!Wrap Toggled!
Example:
1. 1
2. 2
Indexing Accesses character at a specific index.
1. my_string="Hello"
2. char = my_string[0]
Copied!Wrap Toggled!
len() Returns the length of a string. Syntax:
1. 1
1. len(string_name)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
1. my_string="Hello"
2. length = len(my_string)
Copied!Wrap Toggled!
Example:
1. 1
2. 2
lower() Converts string to lowercase.
1. my_string="Hello"
2. uppercase_text = my_string.lower()
Copied!Wrap Toggled!
Example:
1. 1
2. 2
print() Prints the message or variable inside `()`.
1. print("Hello, world")
2. print(a+b)
Copied!Wrap Toggled!
Python Operators - Addition (+): Adds two values together. Example:
- Subtraction (-): Subtracts one value from another.
- Multiplication (*): Multiplies two values. 1. 1
- Division (/): Divides one value by another, returns a float.
2. 2
- Floor Division (//): Divides one value by another, returns the quotient as an integer.
- Modulo (%): Returns the remainder after division. 3. 3
4. 4
5. 5
6. 6
7. 7
1. x = 9 y = 4
2. result_add= x + y # Addition
3. result_sub= x - y # Subtraction
4. result_mul= x * y # Multiplication
5. result_div= x / y # Division
6. result_fdiv= x // y # Floor Division
7. result_mod= x % y # Modulo</td>
Copied!Wrap Toggled!
Example:
1. 1
2. 2
replace() Replaces substrings. 1. my_string="Hello"
2. new_text = my_string.replace("Hello",
"Hi")
Copied!Wrap Toggled!
Syntax:
1. 1
1. substring = string_name[start:end]
Copied!Wrap Toggled!
Example:
Slicing Extracts a portion of the string.
1. 1
1. my_string="Hello" substring =
my_string[0:5]
Copied!Wrap Toggled!
split() Splits string into a list based on a delimiter. Example:
1. 1
2. 2
1. my_string="Hello"
2. split_text = my_string.split(",")
Copied!Wrap Toggled!
Example:
1. 1
2. 2
strip() Removes leading/trailing whitespace.
1. my_string="Hello"
2. trimmed = my_string.strip()
Copied!Wrap Toggled!
Example:
1. 1
2. 2
upper() Converts string to uppercase.
1. my_string="Hello"
2. uppercase_text = my_string.upper()
Copied!Wrap Toggled!
Syntax:
1. 1
1. variable_name = value
Copied!Wrap Toggled!
Example:
Variable Assignment Assigns a value to a variable. 1. 1
2. 2
1. name="John" # assigning John to variable
name
2. x = 5 # assigning 5 to variable x
Copied!Wrap Toggled!