1.3.1

Data Types

Test yourself

Data Types

Every piece of data that is stored has a type. The type determines how it is stored and what you can do with the data.

Illustrative background for IntegerIllustrative background for Integer ?? "content

Integer

  • Integers are whole numbers.
    • E.g. 12
    • E.g. 0
    • E.g. -142
Illustrative background for FloatIllustrative background for Float ?? "content

Float

  • Floats are fractional numbers.
    • E.g. 0.42
    • E.g. 1.00
Illustrative background for StringIllustrative background for String ?? "content

String

  • Strings are text data.
    • E.g. "Hello, world!"
    • E.g. "07123 456 789"
Illustrative background for BooleanIllustrative background for Boolean ?? "content

Boolean

  • A True or False value.
  • Examples:
    • True.
    • False.

Casting

Casting is the process of converting data from one type to another.

Illustrative background for Reasons for castingIllustrative background for Reasons for casting ?? "content

Reasons for casting

  • One of the most common reasons for casting is output.
  • Output must be formatted as a string, and so we may need to convert a certain piece of data to a string.
  • All input also comes as a string, and must then be converted to other data types.
Illustrative background for Casting functionsIllustrative background for Casting functions ?? "content

Casting functions

  • Casting to a string can be done using the str function. E.g:
    • str(3) gives "3".
  • Casting to an integer can be done using the int function. E.g:
    • int(3.4) gives 3.
  • Casting to a float can be done using the float function. E.g:
    • float("3.4") gives 3.4.

Type Coercion

Type coercion allows us to operate on data of different types.

Illustrative background for Type coercionIllustrative background for Type coercion ?? "content

Type coercion

  • Usually, attempting to use data of different types in an operation (e.g. adding two numbers of different types) will cause an error.
  • Type coercion allows one of the types to be automatically cast so that the operation can be completed.
Illustrative background for Integers and floatsIllustrative background for Integers and floats ?? "content

Integers and floats

  • When working with a float and an integer, the final result will become a float.
    • E.g: 3 + 2.5 = 5.5
Illustrative background for Strings and integersIllustrative background for Strings and integers ?? "content

Strings and integers

  • When working with a string and an integer, the final result will become a string.
    • E.g: "Number = " + 3 = "Number = 3"

Jump to other topics

1Introduction to Python

2Data Structures

2.1Lists & Strings

2.2File Handling

3Modularity

3.1Subroutines

3.2Data Flow

4Good Practice

4.1Naming & Whitespace

4.2Comments

4.3Exceptions

Go student ad image

Unlock your full potential with GoStudent tutoring

  • Affordable 1:1 tutoring from the comfort of your home

  • Tutors are matched to your specific learning needs

  • 30+ school subjects covered

Book a free trial lesson