Search this site
Embedded Files
Software Engineering
  • Home
  • Preliminary
    • Programming Fundamentals
      • Project 0 - Intro
        • Software Development Process
        • Waterfall Vs Agile
      • Project 1 - Mad Libs
        • Lab 1 - Hello World
        • Lab 2 - Hello, name
        • Lab 3 - Hello, full name
        • Lab 4 - Split String
      • Project 2 - Temperature Converter
        • Lab 1 - Numbers
        • Lab 2 - Number Operations
        • Lab 3 - Formatting Numbers
        • Lab 4 - Conditionals
      • Project 3 - Guessing Game
        • Lab 1 - While Loops
        • Lab 2 - For Loops
        • Lab 3 - Input Validation
        • Lab 4 - Random
      • Project 4 - Dice Roller
        • Lab 1 - Functions
        • Lab 2 - Parameters and Arguments
        • Lab 3 - Try / Except
      • Project 5 - To Do List
        • Lab 1 - Lists
        • Lab 2 - Advanced List Features
        • Lab 3 - Errors
      • Project 6 - Shopping List
        • Lab 1 - Dictionaries
        • Lab 2 - Lists and Dictionaries
        • Lab 3 - Data Dictionaries
      • Project 7 - Tic Tac Toe
        • Lab 1 - Intro to Connect Four
        • Lab 2 - IPO Diagram and Screen Design
        • Lab 3 - Structure Chart and Modular Design
        • Lab 4 - Starting Development & Basic Gameplay
        • Lab 5 - Core Game Mechanics
        • Lab 6 - Win and Draw Conditions
      • Project 8 - Contacts
    • Object Oriented Programming
      • Project 9 - Contacts (OOP)
        • Lab 1 - Paradigms
        • Lab 2 - Classes
        • Lab 3 - Methods
        • Lab 4 - Contacts Manager
      • Project 10 - Animal Kingdom
        • Lab 1 - Generalisation & Inheritance
        • Lab 2 - Polymorphism
        • Lab 3 - Zoo Management System
      • Project 11 - Shapes & Geometry
      • Project 12 - Inventory System
        • Lab 1 - Encapsulation Revisited
        • Lab 2 - The Facade Pattern
        • Lab 3 - User Interface
      • Project 13 - Banking System
      • Project 14 - Library System
        • Lab 1 - Basic Text IO
        • Lab 2 - Introduction to JSON
        • Lab 3 - Library System
      • Project 15 - D&D Character Creator
    • Programming Mechatronics
      • Intro to Mechatronics
        • Applications
        • Components
        • Microbit
      • Computer Architecture
        • Microcontrollers vs CPUs
        • Instruction Sets and Opcodes
        • Registers and Real-Time Systems
      • Sensors and Data
        • Sensor Basics
        • Compass
        • Thermostat
        • Mapping and Clamping
        • Night Light
        • Parking Sensors
      • Actuators and Mechanics
        • Actuator Basics
        • Actuator Selection
        • DC Motors
        • Servos
        • Grippers
        • Pick and Place
      • Control Systems
        • Intro to Control Systems
        • Bang-Bang Control
        • PID Control
        • Autonomous Control
      • Power, Electrical and Wiring
        • Power Requirements
        • Electrical and Wiring
  • HSC
    • Programming For The Web
      • Command Line Basics
      • HTML
        • Task 1 - Basic Page
        • Task 2 - Contact Form
        • HTML Quiz
      • CSS
        • Task 1 - Style Basic Page
        • Task 2 - Style Contact Form
        • CSS Quiz
      • Bootstrap
        • Task 1 - Basic Bootstrap Page
        • Task 2 - Bootstrap Contact Form
        • Bootstrap Quiz
      • Flask
        • Task 1 - Installation
        • Task 2 - Routes
        • Task 3 - Templates
        • Task 4 - Flask Contact Form
        • Flask Quiz
      • SQLite
        • Task 1 - SQL Basics
        • Task 2 - SQLite Basics
        • Task 3 - SQLite and Flask
        • Task 4 - Object-Relational Mapping
        • SQLite Quiz
      • The Internet
        • Data Transfer
        • Web Protocols
        • Dev Tools
        • Securing the Web
        • Big Data
        • Internet Quiz
      • PWA Assessment Task
        • Progressive Wep Apps
        • Project Setup and Planning
        • Backend Development with Flask
        • User Interface and Design
        • Application Features
        • PWA Features
    • Secure Software Architecture
      • Introduction
        • Unsecure PWA
        • Vulnerabilities
        • Cybersecurity Concepts
        • Case Studies
      • Benefits
        • Data Protection
        • Confidentiality, Integrity, Availability
        • Minimising Cyber Attacks and Vulnerabilities
        • Authentication, Authorisation, Accountability
      • Software Development Steps
      • Influence of End Users
      • Security Features Incorporated Into Software
      • Cryptography and Sandboxing
      • Testing and Evaluating Security and Resilience
        • SQL Injection
        • Cross Site Scripting
        • Broken Authentication
        • Cross Site Request Forgery
      • Design, Develop and Implement Secure Code
    • Software Automation
      • Introduction
      • Labs
        • Numpy
        • Matplotlib
        • Scikit - Linear Regression
        • Scikit - Logistic Regression
        • Scikit - KNN
        • Scikit - Polynomial Regression
      • Training Models
        • Supervised Learning
        • Unsupervised Learning
        • Semi-Supervised Learning
        • Reinforcement Learning
      • Applications of Key ML Algorithms
      • Decision Trees and Neural Networks
        • Decision Trees
        • Neural Networks
      • Machine Learning Algorithms
        • Linear Regression
        • Logistic Regression
        • K-Nearest Neighbour
      • Impact of Automation
    • Software Engineering Project
  • AI Tools
  • Software
  • Syllabus
  • Exam
Software Engineering

PROJECT 2 - TEMP CONVERTER

LAB 3 - Formatting Numbers

Lab 3 Overview

Now that we know how to work with numbers, including performing mathematical operations, let's learn how to format them better for display.

TASK 1

  1. Open lab3.py from Project 2 - Temp Converter

Rounding Numbers

One of the most common things you will need to do is round a number to a fixed amount of decimal places.
For this, we can use the round() function. Notice how you can specify the number of decimal places, or just round to an integer.

Formatting Large Numbers

You may have a large number which would be more readable with commas as thousands separators.
Here we just use an f-string as normal, but after the variable place a colon and comma. Like this :,

Scientific Notation

You may have a very large or very small number which would be better presented with scientific notation.
Here we just use an f-string as normal, but after the variable place a colon, the number of significant figures and then 'e'. Like this :.2e

More Formatting

This is just the tip of the iceberg. There is a lot you can do with f-strings to format numbers (and other data types).
Here are just a couple of more examples before you attempt the lab.

TASK 2

  1. Complete the following requirements:

    • Ask the user for a decimal number and store it in a variable 'num1', converting to a float

    • Round the number to 2 decimal places using the round function

    • Define a large integer

    • Print the large integer with commas as thousands separators using f-strings

    • Define a small floating-point number

    • Print the number in scientific notation using f-strings

    • Define a small integer

    • Print the number with leading zeros using f-strings

  2. Press Ctrl + F5 to run the program or open a terminal and type python lab3.py

  3. It should run without errors and you should see something like on the right:

Example:

Please enter a decimal number: 7.4398


Rounded to 2 decimal places: 7.44


Large Number: 1,234,567,890


Small Number: 1.23e-04


Leading Zeros: 00042

You should now know how to format numbers in a variety of ways in Python.
Let's wrap up this lab by pushing our code to GitHub:

TASK 3

  1. Enter a commit message. E.g. "Lab 3 complete"

  2. Press Commit to main

  3. Press Push origin

Report abuse
Page details
Page updated
Report abuse