CC102
REVIEWER
Variable
A container for a value (string, integer, float, boolean). A variable behaves as if it was the
value it contains.
Examples:
Strings
first_name = “Bro”
food = “pizza”
email = “Bro123@fake.com”
Integers:
age = 25
quantity = 3
num_of_students = 10
(Integers are whole numbers. Make sure they are not within quote because
then technically they would be a string.)
Float
price = 19.99
gpa = 3.2
distance = 5.5
Typecasting
The process of converting a variable from one data type to another. Str(), int(), Float(), bool().
Example:
name = “Bro Code”
age = 25
gpa = 3.2
is_student = True
print(type(name)
Result: <class ‘str’>
Input( )
A function that prompts the user to enter data. Returns the entered data as a string.
Example:
name = input("Please enter your name: ")
print(f"Hello, {name}!")
Arithmetic & Math
Example:
Comparison Operators:
Equal to (==): Checks if two values are equal.
o Example: 5 == 5 is True, 5 == 6 is False.
Not Equal to (!=): Checks if two values are not equal.
o Example: 5 != 5 is False, 5 != 6 is True.
Greater Than (>): Checks if the first value is greater than the second value.
o Example: 6 > 5 is True, 5 > 6 is False.
Less Than (<): Checks if the first value is less than the second value.
o Example: 5 < 6 is True, 6 < 5 is False.
Greater Than or Equal To (>=): Checks if the first value is greater than or equal
to the second value.
o Example: 6 >= 5 is True, 5 >= 6 is False, 5 >= 5 is True.
Less Than or Equal To (<=): Checks if the first value is less than or equal to the
second value.
o Example: 5 <= 6 is True, 6 <= 5 is False, 5 <= 5 is True.
Arithmetic operators
Addition (+): Adds two values together.
o Example: 5 + 3 equals 8.
Subtraction (-): Subtracts the second value from the first value.
o Example: 5 - 3 equals 2.
Multiplication (*): Multiplies two values together.
o Example: 5 * 3 equals 15.
Division (/): Divides the first value by the second value.
o Example: 5 / 3 equals 1.6666666666666667.
Modulo (%): Returns the remainder of a division.
o Example: 5 % 3 equals 2.
Exponentiation ()**: Raises the first value to the power of the second value.
o Example: 5 ** 2 equals 25.
If, else, and elif statements
If-statements: Conditions are evaluated as True or False. If the condition is True, the identified
block of code runs.
Else Statements: block runs if the condition in the If statement is False.
Elif statement: Allows checking multiple conditions sequentially. If the first condition is False,
Python checks the elif condition, and so on.
Examples:
Example 1: Checking for a specific condition
temperature = 25
if temperature > 30:
print("It's a hot day!")
else:
print("It's a pleasant day.")
Example 2: Checking for multiple conditions using elif
score = 85
if score >= 90:
print("Excellent!")
elif score >= 80:
print("Very good!")
elif score >= 70:
print("Good job!")
else:
print("Keep trying!")
Example 3: Using if statements for decision-making
age = 17
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not yet eligible to vote.")
Example 4: Combining if statements with logical operators
is_raining = True
is_sunny = False
if is_raining and not is_sunny:
print("It's raining, and the sun is not out.")
else:
print("It's either not raining, or the sun is shining.")
KEYWORDS EXAMPLES
And
As
Assert
Break
Class
Continue
Def
Elif
Else
Except
False
Finally
For
From
If
Import
In
Lambda
None
Nonlocal
Not
Or pass
Raise
Return
Try
While
With
Yield
STRING METHOD
Upper()
Lower()
Replace()
Find()
Split()
TYPE OF CONVERSION
1. Explicit Type Conversion (Type Casting):
Explicit type conversion, also known as type casting, is where you explicitly tell the compiler or
interpreter to change the data type of a variable or value. You use built-in functions or operators provided
by the programming language.
Example:
# Integer to float
num1 = 10
num2 = float(num1) # num2 becomes 10.0 (a float)
# String to integer
str_num = "25"
int_num = int(str_num) # int_num becomes 25 (an integer)
2. Implicit Type Conversion (Type Coercion):
Implicit type conversion, or type coercion, happens when the compiler or interpreter
automatically converts a value from one data type to another without you explicitly telling it to do so. It
usually occurs when the context of the operation requires a specific data type.
Example:
# Integer and float addition
num1 = 10
num2 = 2.5
result = num1 + num2 # Result is 12.5 (a float)
DATA TYPES
Primitive Data Types:
Primitive data types are the basic building blocks of data in a programming language. They
represent simple values that are not composed of other data types.
Examples:
o Integer (int): Whole numbers (e.g., 10, -5, 0).
o Float (float): Numbers with decimal points (e.g., 3.14, -2.5).
o Character (char): Single characters (e.g., 'A', '!', '9').
o Boolean (bool): Represents truth values (e.g., True, False).
Non-Primitive Data Types:
Non-primitive data types are built upon or composed of primitive data types. They represent
complex structures or collections of data.
Examples:
o String (str): A sequence of characters (e.g., "Hello world", "Python").
o List (list): An ordered collection of items (e.g., [1, 2, 3], ["apple", "banana",
"cherry"]).
o Tuple (tuple): An immutable (unchangeable) ordered collection of items (e.g., (1,
2, 3), ("apple", "banana", "cherry")).
o Dictionary (dict): A collection of key-value pairs (e.g., {"name": "Alice", "age":
30}).
o Set (set): An unordered collection of unique items (e.g., {1, 2, 3}, {"apple",
"banana"}).
WORKING WITH STRINGS
Strings are a critical data type and have various operations for handling text and data.
String slicing
String concatenation
String formatting
OPERATORS AND EXPRESSIONS
Operators
Operators are special symbols that perform specific operations on values or variables. They are
the "verbs" of your code, telling the computer what to do with the data.
Types of Operators:
Arithmetic Operators: These deal with mathematical calculations.
Symbol Operation Example Result
+ Addition 5+3 8
- Subtraction 10 - 4 6
* Multiplication 2*6 12
/ Division 15 / 3 5
% Modulo (remainder) 17 % 5 2
** Exponentiation 2 ** 3 8
Comparison Operators: Used to compare values and determine their
relationship.
Symbol Operation Example Result
== Equal to 5 == 5 True
!= Not Equal to 5 != 6 True
> Greater Than 10 > 5 True
< Less Than 5 < 10 True
>= Greater Than or Equal To 10 >= 10 True
<= Less Than or Equal To 5 <= 10 True
Logical Operators: Combine or modify Boolean (True/False) expressions.
Symbol Operation Example Result
and Logical AND True and True True
or Logical OR True or False True
not Logical NOT not True False
Assignment Operators: Used to assign values to variables.
Symbol Operation Example
= Assignment x = 10
+= Add and Assign x += 5 (equivalent to x = x + 5)
-= Subtract and Assign x -= 2 (equivalent to x = x - 2)
*= Multiply and Assign x *= 3 (equivalent to x = x * 3)
/= Divide and Assign x /= 2 (equivalent to x = x / 2)
%= Modulo and Assign x %= 3 (equivalent to x = x % 3)
**= Exponentiation and Assign x **= 2 (equivalent to x = x ** 2)
Expressions
Expressions are combinations of values, variables, operators, and function calls that are evaluated
to produce a single value. They are the "phrases" of your code, representing calculations or comparisons.
Examples:
o 10 + 5 (arithmetic expression)
o x > 100 (comparison expression)
o not (a and b) (logical expression)
o x = 5 (assignment expression)
o my_function(2, 3) (function call expression)
How Operators and Expressions Work Together:
Operators are the building blocks of expressions. They combine the values and variables
to produce a result.
Expressions are evaluated by the computer to determine their value. This value can then
be used in other parts of your program.
Example:
x = 10
y=5
result = x + y # Arithmetic expression
print(result) # Output: 15
is_greater = x > y # Comparison expression
print(is_greater) # Output: True
RANGE
The range() function generates a sequence of numbers. It's often used to control loops.
Syntax: range(start, stop, step)
o start: The starting number of the sequence (optional, defaults to 0).
o stop: The number to stop before (exclusive).
o step: The increment between numbers (optional, defaults to 1).
Examples:
# Generate numbers from 0 to 4 (inclusive)
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
# Generate numbers from 2 to 10 (inclusive), with a step of 2
for i in range(2, 11, 2):
print(i) # Output: 2, 4, 6, 8, 10
# Generate numbers in reverse order (from 10 to 1)
for i in range(10, 0, -1):
print(i) # Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
LEN
The len() function returns the length (number of elements) of a sequence (like a string,
list, tuple, or dictionary).
Syntax: len(sequence)
Examples:
my_string = "Hello"
print(len(my_string)) # Output: 5
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # Output: 5
my_tuple = ("apple", "banana", "cherry")
print(len(my_tuple)) # Output: 3
my_dict = {"name": "Alice", "age": 30}
print(len(my_dict)) # Output: 2 (number of key-value pairs)
Using range and len Together:
Iterating over a sequence:
my_list = ["apple", "banana", "cherry"]
# Iterate through the list using its length
for i in range(len(my_list)):
print(my_list[i]) # Output: apple, banana, cherry
o Creating a range based on the length of a sequence:
python
my_string = "Python"
string_length = len(my_string) # string_length is 6
# Create a range up to the length of the string
for i in range(string_length):
print(i) # Output: 0, 1, 2, 3, 4, 5
Key Points:
range creates a sequence of numbers, while len tells you the size of a sequence.
range is often used with for loops to iterate over a specific number of times.
len is helpful when you need to know the length of a sequence for calculations or to
control loops.
This is all I remember, but I hope it helps:)
-Honey, R.