Python core concepts
1. Variables
Section titled “1. Variables”var1 = "hello"item = "Code 101"print(type(item))2. Comments
Section titled “2. Comments”# Example of a single-line comment3. Numbers (int and float)
Section titled “3. Numbers (int and float)”When comparing floats directly, we may run into precision issues:
0.1 * 3 == 0.3 # FalseTo tackle this, we can use the math.isclose() function:
import math
math.isclose(0.1 * 3, 0.3) # True4. Arithmetic Operations
Section titled “4. Arithmetic Operations”+,-,*,//true division -> float//floor division -> integer or float%modulo -> remainder**-> power
5. Strings
Section titled “5. Strings”- Concatenation (
+): Joins strings. - Length (
len()): Gets the number of characters. - Indexing (
[]): Access a character by position (0-based). - Slicing (
[:]): Extract substrings. .lower() / .upper().strip() / .lstrip() / .rstrip().startswith() / .endswith().split() / .join().replace()