Unit 8.1 - Slides
Unit 8.1 - Slides
Constants
By the end of the lesson, I will understand the
types and uses of variables and constants in
programming.
Computer Science - Unit 8.1 (Lesson 1)
Key Vocabulary
Variable: A storage location in a program that can hold data that may change
during execution.
Constant: A storage location with a fixed value that cannot be changed during
program execution.
Identifier: A name given to a variable or constant to identify it in the code.
Data Type: A classification that specifies what kind of data a variable can hold.
Boolean: A data type that can only hold one of two values: true or false.
What do we already know?
>>Goto CodeSkulpter3
Enter:
Data types specify what kind of data a variable can hold. Common
types include Integer for whole numbers, Real for decimal values,
Character for single letters or symbols, String for sequences of
characters, and Boolean for true or false values. Knowing these
types is essential when programming.
Integer Data Type
The Boolean data type holds one of two values: true or false. This
data type is essential in programming for conditional statements,
which help in making decisions. For example, a condition can
check if a user is logged in (true) or logged out (false).
Practice
Enter: This code defines two boolean values,
performs logical operations (AND and OR),
# Define two boolean values and checks their type. The output will
is_true, is_false = True, False confirm they are booleans.
Requirements:
● Use float for money-related inputs.
● Use integer for the quantity of items.
● Use a boolean to check if the user has enough money.
Starter Code
# Step 1: Get user's name # Step 5: Determine if they have enough
money (boolean)
name = input("What is your name? ")
can_afford = money >= total_cost
1. Apply a Discount:
○ If the user’s total cost (before applying discounts and tax) is above a certain threshold (e.g.,
$100), apply a 10% discount.
○ Calculate the discounted total if applicable.
2. Add Sales Tax:
○ Apply a sales tax of 5% to the final amount after the discount (if any).
3. Ask if They Want to Buy Another Item:
○ After displaying the result, ask if the user wants to add another item to their shopping list.
○ If the user says yes, repeat the process, updating the total cost each time.
○ If they say no, display the final total, including discounts and tax.
4. Final Output:
○ Display the subtotal, discount (if applied), tax, and the final total cost.
○ Confirm if they can afford the purchase based on the final total.
Questions
1. What is a variable in programming?
2. Why are constants used in programming?
3. What are the different data types mentioned in the lesson?
4. How does an identifier help in programming?
5. Can you explain what a Boolean data type is used for?
Answers
1. A variable is a storage location in a program that can hold data that may
change during execution.
2. Constants are used for fixed values that should remain the same throughout
the program.
3. The different data types include Integer, Real, Character, String, and Boolean.
4. An identifier helps in programming by giving meaningful names to variables
and constants, improving code readability.
5. The Boolean data type is used for conditions in programming, allowing for
true or false values in decision-making.
Understanding Input, Output, and
Control Structures in Programming
By the end of the lesson I will know how to
implement and manage input and output in
programmes, and understand control structures
in programming.
Computer Science - Unit 8.1 (Lesson 1)
Key Vocabulary
Algorithm: A step-by-step procedure or formula for solving a problem.
Input: Data or commands that are entered into a computer system for
processing.
Output: Information that is produced by a computer after processing data.
Control Structure: A programming construct that determines the flow of
control in a program.
Iteration: The process of repeating a set of instructions until a condition is met.
What do we already know?