# [SECTION] Comments in Python # In Python, comments are written using the "#" symbol. # Remember that comments are not read by the program; they are only for the user. # Comments can also be written inline. # [SECTION] Python Syntax # print() is a built-in function that displays text or other data in the terminal. print("Hello world!") # Result: Hello World! # Like JavaScript, Python does not require semicolons at the end of statements. # To run a Python program: # Windows and Linux # python discussion.py # MacOS # python3 discussion.py # print("Hello World!") # Result: IndentationError: unexpected indent # In many other programming languages, indentation is used only for readability, but in Python, it is essential because it indicates a block of code. # [SECTION] Naming convention # The terminology used for variable names is "identifier." # All identifiers should begin with a letter (A–Z or a–z) or an underscore (_). # After the first character, identifiers can have any combination of characters. # Unlike JavaScript, which uses camelCase, Python follows the snake_case convention for variable names, as defined in PEP 8 (Python Enhancement Proposal 8). # Keywords cannot be used as identifiers. # Most importantly, identifiers are case-sensitive. age = 20 middle_initial = "C" # [SECTION] Data Types # Data types convey what kind of information a variable holds. There are different types, each with its own purpose. # In Python, these are the commonly used data types: # Strings (str) - for alphanumeric characters and symbols full_name = "John Doe" secret_code = "Pa$$w0rd" # Numbers (int, float, complex) - for integers, decimals, and complex numbers num_of_days = 365 pi_approx = 3.1416 complex_num = 1 + 5j # This is a complex number, j represents the imaginary component # Boolean (bool) - for truth values is_learning = True is_difficult = False # [SECTION] Using variables # After declaring variables, they can be used by calling the identifier. print(full_name) # Result: John Doe # Python allows assigning values to multiple variables in a single line: name1, name2, name3, name4 = "John", "Paul", "George", "Ringo" print(name1) # Result: John print(name2) # Result: Paul print(name3) # Result: George print(name4) # Result: Ringo # In Python, the "+" symbol can be used to concatenate strings. print("Concatenation in Python:") print("My name is " + full_name) # Result: My name is John Doe # print("My age is " + age) # Result: TypeError: can only concatenate str (not "int") to str # [SECTION] Typecasting in Python # There may be times when you want to specify a type for a variable. This can be done using casting. Here are some functions you can use: print("Typecasting in Python:") # str() - converts the value into string print("My age is " + str(age)) # Result: My age is 20 # float() - converts the value into float print(float(age)) # Result: 20.0 # int() - converts the value into integer print(int(pi_approx)) # Result: 3 # Another way to avoid the type error without using typecasting is by using an f-string. # To use an f-string, add a lowercase f before the string, and place the variable you want to display inside curly braces {}. print(f"Hi, my name is {full_name}, and I am {age} years old.") # Result: Hi, my name is John Doe, and I am 20 years old. # [SECTION] Operations # Python has families of operators that can be used to manipulate variables. # Arithmetic operators - performs mathematical operations print("Arithmetic Operators:") print(1 + 10) # Result: 11 print(15 - 8) # Result: 7 print(18 * 9) # Result: 162 print(21 / 7) # Result: 3.0 print(18 % 4) # Result: 2 print(2 ** 6) # Result: 64 # Assignment Operators - used to assign or update variable values print("Assignment Operators:") num1 = 3 num1 += 4 print(num1) # Result: 7 # Note: Other assignment operators include -=, *=, /=, %= # Comparison Operators - used to compare values (returns a boolean value) print("Comparison Operators:") print(1 == 1) # Result: True # Note: Other comparison operators include !=, >=, <=, >, < # Logical Operators - used to combine or evaluate multiple conditions print("Logical Operators:") print(True and False) # Result: False print(not False) # Result: True print(False or True) # Result: True