Back

Activity 1.1 Fundamental Programming Operations

divider

Introduction

Activity 1.1

Fundamental Programming Operations

Topics

  • print() for displaying output
  • Data Types
  • Integer and float data types
  • String data type
  • Variables
  • Arithmetic Operations
  • Comments

The print() Function

print() is a function that displays a message in an application.

print("Hello world!")

The print() Function

print() is a function that displays a message in an application.

print("Hello world!")
Sample
Hello world!

Demo: Display a message using print()

print("I love Python!")
Demo
I love Python!

Passing Multiple Arguments

Use commas to separate the arguments you pass into the print() function.

print("Students:", "Anthony,", "Bob")
Sample
Students: Anthony, Bob

Data Types

A classification that defines the kind of data a variable can hold.

Integer and Float Data Types

In python, numbers are classified as either integers or floating-point (decimal) values.

2.5  401  -65  -123.45  1_000_000

Demo: Print Some Numbers

Use the print() function to display some numbers on the screen.

app.py
print(-1953)
print(1_000_000)

The String data type

The string data type represents a sequence of characters used for text.

Double Quotes: "Hello friend."

Single Quotes: 'Mortimer rules'


I am happy! "I am sad!

Format String (f-string)

Special strings that you easily insert code inside text by using curly braces ({ }).

print(f"I am 35 years old, which is {35 * 12} months.")

Format String (f-string)

Special strings that you easily insert code inside text by using curly braces ({ }).

print(f"I am 35 years old, which is {35 * 12} months.")
Terminal window
I am 35 years old, which is 420 months.

Introduction to Variables

Variables allow us to store data during our program's execution. Sometimes, variables are described as containers for storing information.

Declaring a Variable

You can create a variable and assign it a value whenever you need one.

health = 100

Declaring a Variable

A variable can be used anywhere a typical value would be used.

health = 100
print("Health:", health)
Terminal window
Health: 100

The Assignment Operator

The equal sign (=) used to assign a value to a variable.

It is not used to check for equality; it is only used for assigning a value to a variable.

Variable Scope

Variables must be declared before they can be used.

print("Health:", health)
health = 100

Demo: Declare and Use Variables

score = 14000
lives = 3
vehicle = "Motorcycle"
print(vehicle)
print("Lives left:", lives)
print("Score:", score)

Demo: Declare and Use Variables

score = 14000
lives = 3
vehicle = "Motorcycle"
print(vehicle)
print("Lives left:", lives)
print("Score:", score)
Terminal window
Motorcycle
Lives left: 3
Score: 14000

Naming Rules and Conventions

Variables names cannot:

  • Contain spaces
  • Start with a number

1st; first name;

For names containing multiple words, use snake_case.

first_name;

Math Operators

+ Addition
- Subtraction
* Multiplication
** Exponents (2**3)
/ Float Division: Divides two numbers and returns the quotient as a decimal.
// Integer Division: Divides two numbers and returns the quotient as an integer.
% Modulus (Mod): Divides two numbers and returns the remainder.

Order of Operations

Use parenthesis for operator precedence.

3 * 4 + 5

3 * (4 + 5)

Demo: Evaluate Some Math Expressions

app.py
x = 5
y = 2
z = 4 * 5
print(x * 10.2)
print(x + y)
print(10 % y)
print(z)
Sample
51
7
0
20

# Comments

Comments serve two key roles: documenting code and disabling it temporarily for testing.

# This line prints a friendly greeting to the console
print("Hello, world!")
# print("This line is disabled for testing purposes.")

# Comments

Comments are completely ignored when the program is executed.

# This line prints a friendly greeting to the console
print("Hello, world!")
# print("This line is disabled for testing purposes.")
Sample
Hello, world!

Key Terms

Data Type
A classification that defines the kind of data a variable can hold, such as numbers and text.
String
A sequence of characters enclosed in quotes, used to represent text in a program.
Integer (int)
A whole number without a fractional or decimal component, such as 3 or -42.
Float (floating-point)
A number that contains a decimal point, used to represent values requiring precision, such as 3.14 or -0.5.

Key Terms

Variable
A named container that stores a value which can be used or changed during program execution.
Assignment Operator (=)
The symbol used to give a variable a value; for example, x = 5 assigns 5 to x.
Arithmetic Operation
A mathematical calculation performed using operators such as +, -, *, /, **, or %.
f-string (formatted string)
A string literal that allows embedded expressions inside curly braces, evaluated at runtime for cleaner output formatting.

Key Terms

print() Function
A built-in Python function that displays text or data on the screen.
Comment
Text in code that Python ignores, used to explain what the program does or to disable code temporarily; written with a # symbol.

'F' → Fullscreen

Objectives

  • icon Work with various basic data types
  • icon Perform arithmetic expressions
  • icon Display output
  • icon Apply formulas from basic algebra
divider

Activity Tasks

  • icon Create a new project named 1-1-data.py.
  • icon Complete each task individually.

Task 1: Printing Simples Messages

2-1-data.py
# Activity 1.1 - Task 1
# Engineer's first Python program
print("Engineering is about solving problems.")
print('Coding helps us solve problems faster.')
print("Let's calculate something useful!")

Task 2: Combining Text and Numbers

2-1-data.py
6 collapsed lines
# Activity 1.1 - Task 1
# Engineer's first Python program
print("Engineering is about solving problems.")
print('Coding helps us solve problems faster.')
print("Let's calculate something useful!")
# Task 2: Combine text and numeric output
print("The area of a 10x5 plate is", 10 * 5, "square cm.")

Task 3: Engineering Units and Conversion

2-1-data.py
10 collapsed lines
# Activity 1.1 - Task 1
# Engineer's first Python program
print("Engineering is about solving problems.")
print('Coding helps us solve problems faster.')
print("Let's calculate something useful!")
# Task 2: Combine text and numeric output
print("The area of a 10x5 plate is", 10 * 5, "square cm.")
# Task 3: Convert inches to centimeters
# 1 inch = 2.54 cm
inches = 8.5
cm = inches * 2.54
print(inches, "inches is equal to", cm, "centimeters.")

Task 4: Using f-strings for Readability

2-1-data.py
18 collapsed lines
# Activity 1.1 - Task 1
# Engineer's first Python program
print("Engineering is about solving problems.")
print('Coding helps us solve problems faster.')
print("Let's calculate something useful!")
# Task 2: Combine text and numeric output
print("The area of a 10x5 plate is", 10 * 5, "square cm.")
# Task 3: Convert inches to centimeters
# 1 inch = 2.54 cm
inches = 8.5
cm = inches * 2.54
print(inches, "inches is equal to", cm, "centimeters.")
# Task 4: Use f-strings to simplify formatting
voltage = 12
current = 1.5
power = voltage * current
print(f"A circuit with {voltage}V and {current}A uses {power}W of power.")

Task 5: Real-World Example: Beam Load Calculation

2-1-data.py
26 collapsed lines
# Activity 1.1 - Task 1
# Engineer's first Python program
print("Engineering is about solving problems.")
print('Coding helps us solve problems faster.')
print("Let's calculate something useful!")
# Task 2: Combine text and numeric output
print("The area of a 10x5 plate is", 10 * 5, "square cm.")
# Task 3: Convert inches to centimeters
# 1 inch = 2.54 cm
inches = 8.5
cm = inches * 2.54
print(inches, "inches is equal to", cm, "centimeters.")
# Task 4: Use f-strings to simplify formatting
voltage = 12
current = 1.5
power = voltage * current
print(f"A circuit with {voltage}V and {current}A uses {power}W of power.")
# Task 5: Simple beam load
# Formula: stress = force / area
force = 500 # Newtons
area = 25 # cm^2
stress = force / area
print(f"The beam experiences {stress} N/cm^2 of stress.")
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
Engineering is about solving problems.
Coding helps us solve problems faster.
Let's calculate something useful!
The area of a 10x5 plate is 50 square cm.
8.5 inches is equal to 21.59 centimeters.
A circuit with 12V and 1.5A uses 18.0W of power.
The beam experiences 20.0 N/cm^2 of stress.
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. When combining text and numbers, which method (commas or f-strings) felt most natural?
  2. Why is commenting important in engineering programs?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete