Python 3a - Data Types

Data Types in Python

If you are a Computer Science student you need to know about the different data types that are used in programming.

  • ​String – A sequence of alphanumeric characters (e.g. “Hello!” or “Toy Story 4” or “Boeing 747”)

  • Integer – A whole number (e.g. -120 or 0 or 340)

  • Float (also called Real) – A decimal number (e.g. -32.12 or 3.14)

  • Boolean – A logical operation (True or False)

  • Character – A single alphanumeric character (e.g. “a” or “6” or “?”) [Not used in Python as it would just be a string with a length of 1]

Converting to Another Data Type

You can convert a variable from one type to another by overwriting the variable with a different data type.

=

On line 1 the variable value of miles is 45.4 (a float - decimal number).

On line 2 the variable data type has been turned into an integer using the int( ) command. 

Because an integer is a whole number, when miles is printed on line 3 it displays 45 with no decimal places.

Other Conversions

str( ) converts a variable to a string.

float( ) converts a variable to a float (decimal number).

Practice Task

1. Ask the user to input a whole number (integer).

2. Convert the number into a float.

3. print the number.

Example solution:

© CSNewbs 2019