Python Fundamentals and Algorithms Explained
Classified in Computers
Written on in English with a size of 5.17 KB
Algorithms
Algorithms involve inputs, instructions, outputs, and a purpose.
Instruction Types
- Instruction
- Decision
- Loop
- Variable
- Function
Python Basics
Comments
Comments explain what code is for, intended for human readers.
Variables
Storing a value in a variable is called assignment. It's best practice not to use generic names.
Calculations (Operators)
+
: Addition-
: Subtraction*
: Multiplication/
: Division//
: Integer Division (e.g.,6 // 4 = 1
)%
: Remainder / Modulo (e.g.,6 % 4 = 2
)**
: Exponentiation
mass_in_kg = 15
weight_in_pounds = mass_in_kg * 2.2
print('the weight in pounds is: ', weight_in_pounds)
user_name = input("enter your user name: ")
print('hello', user_name)
Data Types
- int: Integer numbers (e.g., -1, 0, 2, 1000)
- float: Floating-point (real) numbers (e.g., 3.14159, 2.0)
- str: A text string (e.g., 'A', 'Hello World!'). Anything enclosed by single or double quotes.
- bool: Boolean logic values (
True
,False
).
When mixing int
and float
types in calculations, the outcome is only an int
if both operands are int
.
gas_price = float(input('enter the price of gas: '))
Strings
String Concepts
- Values that never change are called constants (often written in uppercase).
str
: A text string (e.g., 'A', 'Hello World!'). Anything enclosed by single or double quotes.
String Concatenation and Input
first = input('Enter your first name: ')
last = input('Ok ' + first + ', now enter your last name: ')
print('hi ' + first + ' ' + last)
Escape Characters
\n
: New line\t
: Tab
F-Strings (Formatted String Literals)
Easily insert a variable's value into a string.
first_name = 'Willy'
last_name = 'Wonka'
print(f'hi {first_name} {last_name}')
Format Specifiers
Used within f-strings or .format()
.
:.2f
: Format as a floating-point number with 2 decimal places.:%
: Format as a percentage.:10.2f
: Format as a floating-point number with 2 decimal places, right-aligned in a field 10 characters wide.
General format: [alignment][width][,][.precision][type]
String Methods
'hello'.upper()
: Returns the uppercase version ('HELLO').'hello'.lower()
: Returns the lowercase version ('hello').
Functions
A group of instructions that can be grouped together and referred to as a whole. Functions can have inputs (arguments/parameters) and can produce/return outputs.
Types of Functions
- Value-Returning Functions: Produce or return some output (something new).
Example:user_name = input('enter name: ')
- Void Functions (Subroutines): Don't return anything explicitly (often perform an action like printing).
Example:print('hello')
Function Definition Syntax
def function_name(parameter1: type1, parameter2: type2, ...) -> return_type:
# Instructions that use parameters
return something # Optional, for value-returning functions
Void Function Example
def greeting(name: str) -> None:
print(f'Hello {name}')
user_name = input('enter your name: ')
greeting(user_name) # Calling the function with an argument
Value-Returning Function Example
def add_two_integers(x: int, y: int) -> int:
the_sum = x + y
return the_sum
sum = add_two_integers(2, 3) # Calling the function and storing the returned value
print(sum)
Variable Scope (Local vs. Global)
- Local Variables: Defined inside a function and only accessible within that function.
- Global Variables: Defined in the main program body and accessible from anywhere (including inside functions, though modifying them requires the
global
keyword).
Another Function Example (Corrected Syntax)
def add_2_numbers(x: float, y: float) -> None: # Parameters: x, y
sum_result = x + y # Local variable: sum_result
print(sum_result) # Function body
add_2_numbers(2.1, 3.8) # Arguments: 2.1, 3.8