Learn Python Fundamentals
Master the art of mindful coding with our step-by-step foundational tutorials
Variables and Data Type
Variables
Rules for Python variables:
A variable name must start with a letter or the underscore character.
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age, and AGE are three different variables)
Data Type
int: Integer number type. e.g: age = 25
float: Floating-point number. e.g: pi= 3.14
str: Textual data type. e.g: greeting= "Hello Multiverses"
bool: Boolean true/false. e.g: is_valid = True
list: Ordered collection mutable. e.g: fruits = ["Apple", "Banana"]
tuple: Ordered collection immutable. e.g: coordinates = (7, 14)
dict: Key-value pairs. e.g: person = {"name": "Saim", "age": 20}
set: Unordered unique elements. e.g: unique_numbers = {1,2,7,14}
Expressions
Basic Arithmetic Expressions
Use +, -, *, and / for basic operations.result = 5 + 3 * 2 - 1 / 2 # Output: 10.5
Modulus and Exponentiation
% returns remainder, ** raises power.remainder = 10 % 3 # Output: 1 power = 2 ** 3 # Output: 8
String Concatenation and Repetition
Use + to combine strings, and * to repeat.greeting = "Hello" + " World" # Output: "Hello World" repeat = "Hi! " * 3 # Output: "Hi! Hi! Hi! "
Comparison Expressions
Compare values with ==, !=, <, >, <=, >=.is_equal = (5 == 5) # Output: True is_greater = (5 > 3) # Output: True
Logical Expressions
Combine expressions with and, or, not.condition = (5 > 3) and (3 < 7) # Output: True
Membership Tests
Check if an element exists within a container using in or not in.exists = "apple" in ["apple", "banana", "cherry"] # Output: True
Ternary (Conditional) Expressions
Short-hand if-else statements with a if condition else b.age = 18
status = "Adult" if age >= 18 else "Minor" # Output: "Adult"