Institute of Computer Science
  1. Courses
  2. 2024/25 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2024/25 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< previousTable of Contentsnext >

2.3 DATA TYPES

You’ve likely built up some intuition already, as we have briefly touched upon different data types, such as strings, integers, and floating-point numbers. As also mentioned, variables can store data of different types. We need varying data types as each allows us to conduct different operations. Thus, using different data types, we can solve a wider variety of tasks.

NUMBERS

Python's numeric data types include the following.

  • Integers int are whole numbers without a decimal point. Examples would include numbers, such as 54, -12, or 1240
  • Floating-point float are numbers with a decimal point. For example, 1.54, 6.0, -2.3.
  • Complex numbers complex are numbers with a real and imaginary part, for example, 5+2j. Don’t you worry; we won’t be dealing with complex numbers during this course.

STRINGS

Strings str represent sequences of characters for textual data. They can be enclosed in single, double, or triple quotes for multiline strings. It’s important to note that in Python, Strings are immutable, meaning their content cannot be altered after creation.

Remember that for taking user input using the input() function, the default data type is String. We'll cover how to use the input() function below.

TAKING USER INPUT

You can get input from users with the built-in input() function, which pauses the program and waits for the user to type something. When the user presses Enter, input() returns what was typed as a string. The input() function makes Python programs interactive by letting users type in data while the program runs.

The input() function in Python pauses the program's execution until the user provides input. Displaying a message or prompt on the screen to request input from the user is an optional feature of this function. Regardless of the type of input provided by the user, input() always treats it as a string. Therefore, if your input is numeric and you intend to use it as an integer or any other type, you must explicitly convert it using type casting within your code.

If we don’t convert types as necessary, we will get a TypeError.

BOOLEANS

Boolean bool represents truth values, which can be True or False. They are useful, for example, when determining whether one number is greater than another. More on Booleans next week when we learn about Conditional Statements.

OTHER IMPORTANT DATA TYPES

Note that of these data types, we will only explore lists in more depth during the course, but it's good for you to get a quick sense of what other more complex data types exist.

  • Lists list are ordered containers that can store different types of data. The fact that they’re ordered means that Python will keep their elements as inserted. Lists in Python are mutable, meaning that their contents and size can be altered throughout the program.

list_example = ['e', 5, "Introduction to Programming", 5.7]

  • Dictionaries dict store data in key:value pairs, meaning each value in the dictionary has a corresponding key through which the value can be accessed.

dict_example = {
1: "Nike Air Force 1"
2: "New Balance 550"
3: "Adidas Samba"
}

  • Tuples tuple, similarly to lists, are ordered, meaning Python will store the order as defined. However, unlike lists, tuples are not mutable. This means that you cannot change the items in the tuple. You also cannot alter their size after creation.

tuple_example = ("Adidas Gazelle", 150.99, 44)

  • Sets set is also a collection of unique elements, meaning an element can only appear once. However, contrary to lists and tuples, sets are unordered, meaning that Python does not keep track of the order of items, and they can appear randomly. Sets can be modified.

set_example = {1, 2, 3, 4, 5, "Michael Scott"}

TYPE CONVERSION

Python provides built-in tools that allow you to convert values from one data type to another. This is necessary, for example, when asking the user to input a number, as shown in the example above. The value taken by the input() function by default is of type String. Hence we would need to convert it into a number to conduct numerical operations on it.

  • int(), float(), and complex() convert values to respective numeric types.
  • str() converts any data type into a string.
  • For other data types, list(), tuple(), dict(), and set() convert to their respective types.
  • You can use the type() method to check the data type of an object.

# Converting the string to type integer
string_to_integer = int("15")  

# Converting the intger to type string
integer_to_string = str(56)  # Float to string

# Converting the integer to type float
num_float = float(87)  # Integer to float

KEY THINGS TO REMEMBER

  • Numeric data types include integers, floating-point numbers, and complex numbers
  • Strings represent sequences of characters, and they are immutable.
  • Booleans represent truth values True or False.
  • input() function allows to take user input and capture it as a string. To use the input as another type, you must explicitly convert it.
  • Built-in functions like int(), float(), str() allow you to convert values between different data types.

SELF-CONTROL EXERCISES

< previousTable of Contentsnext >

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment