Data Types

Section Vocab:

  • Data type: Lets the computer know what type of information is stored in a variable.
  • Integer: Type for a natural number (has no decimal point).
  • Float: Type for a real number (has a decimal point).
  • String: Type for a word (string of characters).
  • Boolean: A true or false value.

Associated Learning Outcome:

At the end of this module students will be able to:

  • Students will be able to correctly identify data types (such as char, boolean, int, long, and float) in relation to variables.

Data types are a fundamental concept in computer programming that define the type of data that a variable can hold. Each programming language has its own set of data types, but some of the most common ones include:

  1. Integer – used to store whole numbers, such as 1, 2, 3, etc.
  2. Float – used to store decimal numbers, such as 3.14 or 2.5.
  3. String – used to store text, such as “hello” or “world”.
  4. Boolean – used to store either true or false.

When you declare a variable in a programming language, you need to specify its data type. For example, in Python, you can create an integer variable like this:

x = 5

This tells the program to create a variable called “x” and assign it the value 5, which is an integer. If you try to assign a non-integer value to the variable later on, you will get an error.

Similarly, you can create a string variable like this:

name = "John"

This tells the program to create a variable called “name” and assign it the value “John”, which is a string. If you try to assign a numeric value to the variable later on, you will get an error.

Data types are important because they help ensure that the program is working with the correct type of data. If you try to perform an operation on two variables with incompatible data types, you will get an error. For example, if you try to add a string and an integer in Python like this:

x = 5

name = "John"

print(x + name)

Here, the operator ‘+’ tries to concatenate the integer ‘x’ and ‘name’ string.

You will get a TypeError because Python does not allow you to add an integer and a string together.

Try out this example with our calculator below:

Optional

Copy the code from here!

Paste it here (in the little grey box next to [ ]:)!

Press shift+enter or the little play button in the top left corner to run (execute) the program.

Read, watch, and take the assessment:

https://www.learnpython.org/en/Variables_and_Types

https://pythonnumericalmethods.berkeley.edu/notebooks/chapter02.02-Data-Structure-Strings.html (Optional Reading)

References: