Introduction to Control Structures and Data Types

Overview

The simple programs you have written so far simply execute one instruction after another. The sequence is always the same. However, the power of programming is released when your program can decide which commands to execute based on conditions. Logic is used to make the decisions. That’s powerful. 

In addition, we have to deal with the fact that computers only know 0’s and 1’s, otherwise known as the binary number system. Somehow, some way, our programs need to put data in terms of 0’s and 1’s. For example, when you enter your name at a prompt, or even in a text box on a web page form, those letters of the alphabet need to be translated into binary for the computer to store or otherwise process the data. Beyond letters, special characters, and so forth, fractional numbers need to be translated into 1’s and 0’s. That is, numbers with decimal points need to be represented in memory as simple 0’s and 1’s. You don’t need to be an expert on the binary number system in order to write Python programs because Python does the heavy lifting for you through data types. You need to understand data types. 

In this lab, you will explore how to declare variables and use data types in Python. You will also learn how to work with exceptions that Python throws at run time. You will also learn about control structures. Up until now, you have used a sequence structure in your coding. But, wouldn’t it be nice to be to change the flow of a program as it runs. That is where the control structures fit in. In this lab you will learn to use the if...then...else statement. 

outcomes:

In this lab, you will learn to:

  • Declare variables using string, int, float, and Boolean data types.
  • Convert variables from one data type to another.
  • Recognize exceptions that result from mixed data types.

Key terms and descriptions

variables
A variable is a location for storing data such as numbers and strings.
data type
A data type is a way to define the type of data that a variable will contain.
sequence structure
A sequence structure is a group of code that runs without any change in flow.
control structure
A control structure determines the order that statements run. It allows for a change in flow of a program using a control structure.