Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Python Values and Explicit Type Conversion

Recommended resources

Welcome to the first lesson of your course on Inference Engineering. Your journey toward a role in GPU performance engineering begins here, with the fundamental building blocks of programming. Since you're starting with no prior experience in this area, we will build your knowledge from the ground up, ensuring you have a solid and reliable foundation for the more advanced topics to come.

In this lesson, you will learn to write simple Python programs that work with the most basic kinds of data: numbers, text, and true/false values. We'll also cover how to convert data from one type to another, a crucial skill for handling data from different sources. Mastering these concepts is the first essential step toward writing the performance benchmarks and hardware-level code that are central to your career goal.

Numbers and Basic Math in Python

At its core, a computer is a powerful calculator. Much of the work in performance engineering involves measuring and calculating, so our first stop is understanding how Python handles numbers and arithmetic.

Python has two main numeric types you will use constantly:

  • Integers (int): Whole numbers, like -2, 0, or 100.
  • Floating-point numbers (float): Numbers with a decimal point, like -2.5, 0.0, or 3.14159.

You can manipulate these numbers using standard arithmetic operators. To get a formal introduction, please read the following section from the official Python tutorial. It will show you how to use Python as a simple calculator and how to store numbers in variables.

3. An Informal Introduction to Python

This section from the official Python tutorial introduces the int and float numeric types and the basic arithmetic operators.

Please read the subsection "Numbers". Pay close attention to the difference between standard division (/), which always produces a float, and floor division (//), which discards the fractional part. Also, notice the use of ** for exponents and = for assigning values to variables.

As you saw, the operators are mostly intuitive: + for addition, - for subtraction, * for multiplication, and / for division. Storing the result of a calculation in a variable, like result = 5 * 10, allows you to reuse it later.


Working with Text using Strings

Besides numbers, programs constantly handle text. In Python, a piece of text is called a string (or str). Strings are used for everything from printing messages to reading file names and logging benchmark results.

Let's continue with the Python tutorial to see how strings are defined and manipulated.

3. An Informal Introduction to Python

This reading covers the fundamentals of Python strings, including how to create them, combine them, and access their parts.

Please read the entire subsection "Text". Focus on these key ideas: You can create strings using either single ('...') or double ("...") quotes. Strings can be combined (concatenated) with + and repeated with *. Strings are immutable, meaning you cannot change a string once it is created. To "change" a string, you must create a new one. The len() function tells you how many characters are in a string.


Representing Logic with Booleans

The third fundamental data type is the Boolean (bool). It is much simpler than numbers or strings, as it can only have one of two values: True or False. Booleans are the foundation of decision-making in programs. For example, a program might check "is the measured latency greater than the 50ms threshold?" The answer to this question is either True or False, and the program can act accordingly.

Comparison operators like == (equals), != (not equals), > (greater than), and < (less than) are used to compare values and produce a Boolean result.

This short video provides an excellent introduction to Booleans.

Python Booleans || Python Tutorial || Learn Python Programming

This Socratica video explains what Booleans are and how they are used with comparison operators.

Watch the first two minutes of the video, covering the definition of Booleans and how they result from comparisons. What is a Boolean? Note the capitalization: True and False. Using comparison operators to produce Boolean values.


Converting Between Data Types

What happens when you try to combine different types of data, for instance, by adding a number to a string? Python will raise a TypeError because the + operation is ambiguous: should it perform mathematical addition or string concatenation?

To resolve this, you must perform an explicit type conversion. You tell Python exactly how you want to treat the data using built-in functions:

  • int(): Converts a value to an integer.
  • float(): Converts a value to a floating-point number.
  • str(): Converts a value to a string.
  • bool(): Converts a value to a Boolean.

This is a very common task. For example, you might read a latency measurement from a file as a string ("12.34"), convert it to a float (12.34) to perform calculations, and then convert it back to a string to include in a report message.

The following resources explain how and when to use these conversion functions.

Decoding Type Conversions in Python | CodeSignal Learn

This article from CodeSignal Learn provides clear examples for each of the main type conversion functions.

Read the article from the beginning to understand why type conversion is necessary. Then, review the examples for each conversion function. Note what happens when a conversion is not possible, like int("hello"), which results in a ValueError.

The next video provides a great visual walkthrough of converting between numbers and strings, which is one of the most frequent conversions you'll perform.

Numeric Data Types | Python for Beginners [13 of 44]

This video from Microsoft Developer demonstrates the common problem of mixing strings and numbers and how to solve it with str(), int(), and float().

Watch the section on converting numbers to strings using str() to create messages. Then, watch the section on converting strings to numbers using int() and float() to perform math. This is especially important when getting input, which often arrives as a string.

Finally, let's see how values are converted to and from Booleans. The rules are simple but important:

  • For numbers, 0 and 0.0 convert to False. All other numbers convert to True.
  • For strings, the empty string "" converts to False. All other strings convert to True.
  • When converting a Boolean to a number, True becomes 1 and False becomes 0.

You can see this in action in the second half of the Socratica video on Booleans.

Python Booleans || Python Tutorial || Learn Python Programming

This part of the video demonstrates the rules for converting other types to Booleans and vice versa.

Watch the explanation of converting numbers and strings to Booleans. Watch how Booleans are converted to strings and integers. The conversion to 0 and 1 is a pattern you will see again in numerical and GPU programming.


A Practical Example

Let's tie this all together. Imagine you have a program that reports a start_time and an end_time for a task, but it gives them to you as strings. Your goal is to calculate the duration and print it in a human-readable message.

start_time_str = "15"  # Task started at 15 seconds
end_time_str = "37"    # Task ended at 37 seconds




# 1. Convert string times to numbers to do math
start_time_int = int(start_time_str)
end_time_int = int(end_time_str)




# 2. Perform the calculation
duration = end_time_int - start_time_int




# 3. Convert the numeric result back to a string to join it with other strings
message = "Task completed in " + str(duration) + " seconds."




# 4. Print the final message
print(message)

If you run this code, it will print: Task completed in 22 seconds.

This simple example demonstrates the complete cycle: you start with text data, convert it to numbers to perform a calculation, and then convert the result back to text to create a final report. This pattern is fundamental to programming and data processing.


Conclusion

In this introductory lesson, you've taken your first steps into Python programming. You learned about the core data types that form the basis of all programs:

  • Numeric types (int, float) for calculations.
  • String type (str) for handling text.
  • Boolean type (bool) for representing logical True/False values.

You also saw why mixing these types can cause errors and learned how to use the functions int(), float(), str(), and bool() to perform explicit type conversions. Understanding data types is non-negotiable for performance work; later in the course, selecting the right numeric type will be a key factor in maximizing GPU throughput.

In the next lesson, we will build directly on this foundation. You'll learn how to use the True and False Boolean values you saw today to control the flow of your programs with conditionals and loops, making your code dynamic and powerful.