UNIT 6: Fundamentals of Programming
Summary from Grade 11 Textbook
1. Introduction to Programming
Definition: A computer program is a set of instructions that commands a computer what to do.
Purpose: Programs enable computers to perform tasks by following specified instructions.
2. Types of Programming Languages
a. Machine Language
Description: Low-level language consisting of binary code (1s and 0s).
Characteristics: Fast and memory-efficient but difficult to write.
b. Assembly Language
Description: Low-level language using mnemonics instead of binary.
Translation: Requires an assembler to convert to machine language.
c. High-Level Language
Description: Closer to human languages, allowing easier programming.
Examples: Python, Java, C++, C#.
Translation: Uses compilers (translate all at once) or interpreters (translate line by line).
3. Syntax and Semantics
Syntax: The set of rules that defines the structure of a programming language.
Semantics: The meaning of the elements and instructions in a program.
Errors:
o Syntax Errors: Violations of language rules, caught by compilers/interpreters.
o Logic Errors: Programs run without syntax errors but produce incorrect results.
4. Basics of Python
Popularity: Python is widely used for its simplicity and readability.
IDLE: Integrated Development and Learning Environment for writing Python code.
a. Using the Interactive Interpreter
Functionality: Allows execution of one line of code at a time.
Example: Evaluating expressions and using the print() function to display output.
b. Using the Text Editor
Scripts: Python code files saved with a .py extension.
Executing Scripts: Use the "Run Module" option in IDLE to execute saved scripts.
5. Variables and Data Types
a. Variables
Definition: Memory locations to store data.
Assignment: Use the assignment operator (=) to set variable values.
b. Identifiers
Rules: Must begin with a letter or underscore. Cannot be keywords. Case-sensitive.
c. Data Types
Common Types:
o int: Integer numbers
o float: Floating-point numbers
o str: Strings (text)
o bool: Boolean values (True/False)
d. Type Conversion
Implicit Conversion: Automatic conversion by Python to avoid data loss.
Explicit Conversion: Manual conversion using functions like int(), float(), str().
6. Statements and Expressions
Statements: Instructions executed by the interpreter (e.g., assignment, print).
Expressions: Combinations of values and operators evaluated to produce a value.
7. Writing Simple Programs
Input and Output: Use the input() function to accept user input and print() to display
results.
Examples:
1) A program that calculates the area and circumference of a circle based on user-
provided radius.
PI=3.14
x=input(”Enter the radius:”)
radius=float(x)
area=PI*radius**2
circumference=2*PI*radius
print(”The area of the circle is:”, area)
print(”The circumference of the circle is:”, circumference)
2) Write a program that displays the BMI of a person by accepting the weight and height from
the keyboard.
# Accept weight and height from the user
weight = float(input("Enter your weight in kilograms: ")) # Weight in kg
height = float(input("Enter your height in meters: ")) # Height in meters
bmi = weight / (height ** 2) # Calculate BMI
print(f"Your BMI is: {bmi:.2f}") # Display the BMI
# Interpretation of BMI
if bmi < 18.5:
print("You are underweight.")
elif 18.5 <= bmi < 24.9:
print("You have a normal weight.")
elif 25 <= bmi < 29.9:
print("You are overweight.")
else:
print("You are obese.")
Lab Activities
Solve following questions using the fundamentals of Python, particularly focusing on variables,
data types, input/output, and basic calculations:
1. Simple Interest Calculator:
Write a program that accepts the principal amount, rate of interest, and time (in years) from
the user, and calculates the simple interest using the formula:
Simple Interest=P×R×T100\text{Simple Interest} = \frac{P \times R \times T}
{100}Simple Interest=100P×R×T
2. Temperature Converter:
Create a program that converts a temperature given in Celsius to Fahrenheit using the
formula:
F=95×C+32F = \frac{9}{5} \times C + 32F=59×C+32
Ask the user to input the temperature in Celsius.
3. Area of a Rectangle:
Write a program that accepts the length and width of a rectangle from the user and
calculates its area using the formula:
Area=Length×Width\text{Area} = \text{Length} \times \text{Width}Area=Length×Width
4. Grade Calculator:
Create a program that accepts three exam scores from the user, calculates the average score,
and determines the grade based on the average:
o A: 90 and above
o B: 80 to 89
o C: 70 to 79
o D: 60 to 69
o F: below 60
5. Circle Volume Calculator:
Write a program that calculates the volume of a sphere given its radius. Use the formula:
V=43×π×r3V = \frac{4}{3} \times \pi \times r^3V=34×π×r3
where π≈3.14\pi \approx 3.14π≈3.14. Ask the user for the radius.
6. Even or Odd Checker:
Write a program that accepts an integer from the user and determines whether the number is
even or odd.