Introduction to Python
Algorithms, programs, running Python, variables, data types, and basic I/O.
- Explain what an algorithm and a program are
- Write and run a simple Python program
- Declare variables and assign values
- Identify the basic Python data types:
int,float,str,bool,None - Use
print()andinput()for basic I/O - Use the
type()function to check a value's type
Algorithm vs Program
An algorithm is a step-by-step plan (in human language or pseudocode) for solving a problem. A program is the algorithm written in a programming language (like Python) that a computer can execute.
Why Python?
Python is simple to read, widely used in data science, AI and engineering, and is an object-oriented language. It is interpreted β you can run code line by line in the Python shell (IDLE) or run a .py file.
Variables
A variable is a named storage location in memory. You create a variable by assigning a value with =.
name = "Alice"
age = 20
gpa = 3.8
is_enrolled = True
Variable names: start with a letter or _, no spaces, case-sensitive (Age β age).
Python Data Types
| Type | Example | Description |
|---|---|---|
int | 42 | Whole number |
float | 3.14 | Decimal number |
str | "hello" | Text (in quotes) |
bool | True / False | True or False |
NoneType | None | No value / absence of value |
Every value in Python is an object of some class. Use type(x) to check.
print() β Output
print("Hello, World!") # Hello, World!
print("Name:", name) # Name: Alice
print(age, gpa) # 20 3.8
print("Sum:", 3 + 4) # Sum: 7
input() β User Input
input() always returns a string. Convert it if you need a number.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
gpa = float(input("Enter GPA: "))
Comments
Use # for single-line comments. Comments are ignored by Python β they explain the code to humans.
# This is a comment
x = 5 # inline comment
type() and isinstance()
x = 42
print(type(x)) # <class 'int'>
print(type("hello")) # <class 'str'>
print(isinstance(x, int)) # True
Example 1 β First program
# A simple Python program
print("Welcome to ICT582!")
name = input("What is your name? ")
print("Hello,", name)
Example 2 β Variables and types
age = 21
height = 1.75
name = "Bob"
active = True
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(active)) # <class 'bool'>
Example 3 β Input and simple calculation
price = float(input("Enter price: "))
qty = int(input("Enter quantity: "))
total = price * qty
print("Total:", total)
- Know the difference between an algorithm and a program β expect a definition question.
- Be able to identify what data type a value is (
int,float,str,bool). - Know that
input()always returns astrβ you must convert it withint()orfloat()to do arithmetic. - Write a short program that takes user input and prints output β a common exam question.
- Know that Python is an object-oriented language and every value is an object.
- Forgetting to convert
input()βint(input(...))is needed for arithmetic; without it you get string concatenation not addition. - Using undefined variables β you must assign before using:
x = 5before printingx. - Case errors β
TrueandFalsemust start with capital T/F;truecauses a NameError. - Mixing tabs and spaces for indentation β Python is strict; use spaces consistently.
- An algorithm is a plan; a program is the algorithm written in Python.
- Python's basic types:
int,float,str,bool,None. print()outputs;input()reads user text (always a string).- Use
int()orfloat()to convert user input for arithmetic. - Use
type(x)to check a variable's type. - Comments start with
#and are ignored by Python.