Loading...
PCEP-30-02 Module 1: Comprehensive Teaching Material — Python Fundamentals
Table of Contents
- Module Overview & Syllabus
- Introduction to Computer Programming and Python
- What is Python?
- Python Program Structure
- Comments
- Indentation
- Exercises
- Python Literals & Variables
- Literals (int, float, string, bool)
- Variable Naming Rules
- Assignment
- Multiple Assignments
- Exercises
- Numeral Systems
- Decimal, Binary, Octal, Hexadecimal
- Converting Between Systems
- Exercises
- Data Types & Operators
- int, float, str, bool
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Type Casting
- Exercises
- Console Input and Output
- print() Function
- input() Function
- sep and end Parameters
- Using int() and float() for Conversion
- Exercises
- Practice Problems
- Summary Table
1. Module Overview & Syllabus
- Understanding key Python language concepts
- Logic and structure of Python programs
- Working with literals, variables, and data types
- Numeral systems for expressing data
- Using operators for computation and comparisons
- Console input and output
2. Introduction to Computer Programming and Python
What is Python?
- Python is a widely used, high-level, interpreted programming language known for its simplicity and readability.
Python Program Structure
# This is a simple Python program
print("Hello, world!")
- Single-line: use
#
- Multi-line: Python does not have built-in syntax for multi line comments. We can implement multi line comments in python using single line comments or triple quoted python strings (i.e., using triple quotes).
"""
This is a multi-line comment
"""
Indentation
- Indentation is how Python denotes blocks of code.
def greet():
print("Hello!") # Indented inside function
Exercises
- Write a Python program that greets a user by name.
- Write a comment describing what your program does.
3. Python Literals & Variables
Literals
- Integer:
5, 100, -3
- Floating Point:
3.14, 0.0, -2.5
- String:
'hello', "world"
- Boolean:
True, False
Variables
- Container for storing data values.
- Naming rules: letters, digits (not first), underscore, case-sensitive, no reserved words.
user_name = "Alice"
PI = 3.14159
_is_visible = True
Assignment
a = 10
b = a + 5
Multiple Assignments
x, y, z = 1, 2, 3
x = y = z = 0 # Chain assignment
Exercises
- Assign your age, name, and student status to variables.
- Swap values using a temporary variable.
4. Numeral Systems
Decimal, Binary, Octal, Hexadecimal
num_dec = 255 # Decimal
num_bin = 0b11111111 # Binary
num_oct = 0o377 # Octal
num_hex = 0xFF # Hexadecimal
Converting Between Systems
x = 42
print(bin(x), oct(x), hex(x)) # Output: binary, octal, hexadecimal
Exercises
- Convert decimal 100 to binary and hexadecimal.
- Determine the decimal value for 0b1010 and 0xF.
5. Data Types & Operators
Data Types
a = 10 # int
b = 5.5 # float
c = "Python" # str
d = True # bool
print(type(b))
Arithmetic Operators
+, -, *, /, //, %, **
Assignment Operators
=, +=, -=, *=, /=, //=, %=
Comparison Operators
==, !=, <, >, <=, >=
Logical Operators
and, or, not
Type Casting
val = int("5") # str -> int
num = float(7) # int -> float
text = str(True) # bool -> str
Exercises
- Perform arithmetic and comparison on two numbers, show results.
- Cast a string number to integer and multiply by 2.
print() Function
print("Welcome to Python")
print(1, 2, 3, sep="-") # Output: 1-2-3
print("End", end="!
") # Custom ending
name = input("Enter your name: ")
age = int(input("Enter age: "))
Using int() and float() for Conversion
value = int(input("Enter an integer: "))
price = float(input("Enter price: "))
Exercises
- Program asks user for their name and prints a greeting.
- Ask user for two numbers and print their sum.
7. Practice Problems
- Write a program to check the type of variables after assignment.
- Ask user for three numbers and print their average.
- Write a code snippet that shows usage of all arithmetic operators.
- Input a number and print its binary, octal, and hex equivalents.
- Write a program to test if input is greater than 10 and print True/False.
8. Summary Table
| Concept |
Syntax Example |
Notes |
| Literal |
5, "abc", 3.14, True |
Fixed value |
| Variable |
x = 10 |
Named reference |
| Assignment |
a = b + 2 |
Store a value |
| Numeral systems |
0b10, 0o10, 0x10 |
Binary, octal, hexadecimal |
| Data type |
type(x) |
int, float, str, bool |
| Operators |
+, -, *, /, ==, and |
Arithmetic, comparison, logical |
| print() |
print("hi", end="!") |
Printing output |
| input() |
x = input() |
Getting user input |
| Type casting |
int("5"), float("2.7") |
Convert between types |
Use this module to learn all the foundational Python programming concepts needed for the PCEP-30-02 exam. Complete the examples and exercises for robust skills!