Python Printing and Primitive Data Types

Python Printing and Primitive Data Types

Learn data types in Python and how to print them onto the console

Introduction

Python has many different data types that have evolved. There are four primitive data types and many other data types. You can print all of these and most of them are useful even when you are just starting programming with Python.

💡
print (in terms of Python) — displaying some inputted data onto the Python output console.
💡
datatype — a classification of data that tells the compiler or interpreter how the programmer intends to use the data.

Printing Data

In Python, developers like you can print data onto the console of the IDE you are using when you run the program. Python allows you to do this with the built-in print() method.

💡
method — a programmed procedure that is defined as part of a class and is available to any object instantiated from that class.

Here is an example of how to print data onto the console.

print("Hello, World!")
For Developers Who Know Other Languages — if anyone reading this article knows a language like Java or C, you might notice there is no semi-colon at the end of the line. In Python, the interpreter splits the code by newlines or groups of lines if special escape characters are there (we will learn later).

The line of code above with just print 'Hello, World!' onto the console (excluding the quotes). What is in the parenthesis will be covered in the data types section coming right up!

Primitive Data Types

Python has many different data types that help with saving data in formats that will help packages and libraries (external code that can be imported) or other parts of your code run smoothly for the reader. In Python, there are two groups of data types. Primitive and composite data types. We will go over primitive data types in this article. Composite data types will be covered as we go through the articles. There's a lot!

Python has 4 primitive data types. They are strings, integers (ints), floats, and booleans. These data types are in basically every programming language and are the backbone of other complex data types (the composite data types).

💡
Primitive Data Types — A primitive data type is either a data type that is built into a programming language, or one that could be characterized as a basic structure for building more sophisticated data types.

Strings

Strings are the programming term for words. Just like the previous example where I printed 'Hello, World!', that stuff in the parenthesis was a string. A string is defined with double or single quotes ("" or ''). If you didn't have any of the quotes around your word, the output would be a weird-looking error. The interpreter thinks that the words are variables (discussed in the next post) and will look for those in your code. If you don't have the variable defined, the interpreter doesn't know what to do and fails to execute the code. Here is an example of a string.

"My name is NAME" # String saying "My name is NAME"
📢
Side Note — the # signifies a comment. This is ignored by the Python interpreter and is good if someone else is reading your code and needs to understand what's going on. It will even help you with a large project!

If you run the code above, nothing will output. If you have a free-standing string, Python will just take that as a comment too, and ignore it. But things can get confusing so I wouldn't recommend it. To show the string on the console when you run the code, you need to wrap the string around a print() method. Try it yourself before looking at the code below!

print('My name is NAME') # Output - My name is NAME

Notice how I used single quotes this time. As I said before, you can use double quotes OR single quotes to define a string so this code will still run. If you haven't yet, run the code and see the output!

Integers

Integers, also known as ints, are still called integers outside of programming! Integers are numbers without any decimal points. They can be negative or positive. To define an integer, you just have to type the number (e.g. 1, 2, 3, 4, 8, 123, 231, etc.). Here's an example.

1 # Integer defined with value 1

The code above is just the free-standing line of code with 1 and a comment. Just like strings, this won't give an error but nothing will output when this code is run. The interpreter will ignore that piece of code. To show this on the console, you can wrap it in a print() method just like strings. The only thing is that there will be no quotes. Try and do it yourself before looking at the solution below!

print(1) # Output - 1

The above code will output 1!

Floats

Python floats are another important data type (not just because it's primitive). Floats in non-programming terms are called decimals. The only thing different from integers is that floats have decimal points. So the way you define a float is just by typing a decimal number like below.

1.00 # Float defined with value 1.00
1.92832 # Float defined with value 1.92832
0.12341 # Float defined with value 0.12341

The lines of code above show three different decimal numbers that are considered floats. 1.00 is still considered because there is a decimal point. If the number has a decimal point, the interpreter sees it as a float. Even typing '1.' wouldn't give an error because the interpreter would just add a 0 after. The second one is just a normal decimal number so that will work. The final one is also a decimal number because it has a number, a period, and another number. Take note that just doing '.12341' would be fine because the interpreter would add a 0 at the start. These lines of code won't output anything though. Just like the previous two data types when they were free-standing. To print floats, just wrap them in a print() method again. Just like the others. Try doing this yourself with a few different forms of a float before taking a look below.

print(1.00) # Output - 1.0
print(1.92832) # Output - 1.92832
print(0.12341) # Output - 0.12341
print(1.) # Output - 1.0
print(.024821) # Output - 0.024821

Notice in the first print() method, the output is '1.0', not '1.00'. This is because when Python is compiling the code, it removes any leading or trailing zeros. So even printing '000.0123' would result in '0.0123'. Test out these examples if you haven't done so yet!

Booleans

Booleans are the final primitive type in Python. Booleans can only hold two values, true and false. If you've studied binary before and know that any data type takes up some amount of bits, you should know about zeros and ones. Booleans take up only 1 bit and its value can either be 1 for true or 0 for false. In Python, defining a boolean just takes typing one word. Now this word doesn't need to be wrapped in any quotes because it is a reserved word. This means that the Python interpreter knows that this word is not just any word and will take it as the value the interpreter knows it as. The word to type is either 'True' or 'False'. It's case sensitive so watch the capitals. Here is an example.

True # Boolean of True (1)
False # Boolean of False (0)

The first line is the value true. No need to explain that, right? Same with the second line. It speaks for itself. Just like all the other data types, this type won't do anything when free-standing. You just have to print them. Just wrap them in a print() method and run your code!

print(True) # Output - True
print(False) # Output - False

Make sure you remember that you don't need quotes or this will be interpreted as a string! You might be asking, "When are we going to use this?" That is a valid question. Booleans are most used in conditionals, which are covered at the end of basic Python. Stay tuned for that! But in summary, conditionals just check a given value and do something or something else based on that value! They are undoubtedly the most used concepts (along with intermediate loops) in any programming language (aside from web development). Before you go, a nice feature in Python allows you to check the type of some given data. This can be extremely useful if you are asking for some input from the user in some expected type and they give something else. You need to make sure that the type they gave is valid. There are multiple ways to do that, but type-checking is one of them. The next section in this post is about type-checking

Type Checking

Type checking in Python is extremely useful as I just said. To check a type, you need to wrap the data to check in another built-in method called type(). Here is an example.

type("Hello") # Checking a string
type(1) # Checking an Integer
type(1.203) # Checking a Float
type(True) # Checking a Boolean

The lines above show ways to see the type of different data types. When you don't know the data type, you can wrap it in the type() method and check that type with a conditional. Just like the data types, this will not output anything freestanding. To display the type, you need to wrap the data in a type() method and wrap that whole thing in a print() method.

print(type("Hello")) # Output - <class 'str'>
print(type(1)) # Output - <class 'int'>
print(type(1.203)) # Output - <class 'float'>
print(type(True)) # Output - <class 'bool'>

The outputs show the type of the data. You don't need to understand all of it right now, just the words wrapped in a single quote. Use this table to help you understand the words.

Quoted WordMeaning
'str'string
'int'integer
'float'float
'bool'boolean

When checking a type in a conditional, you will only need to remember these quoted words. Just keep track of them as they pop up through the series. Hope you learned something new today. Thank you for staying with me till the end. I know it was a very long article!

Works Cited

“Primitive Data Type.” Techopedia, 19 July 1970, www.techopedia.com/definition/ 29494/primitive-data-type. Accessed 07 Oct. 2023.

Busbee, Kenneth Leroy, and Dave Braunschweig. “Data Types.” Programming Fundamentals, 15 Dec. 2018, press.rebus.community/programmingfundamentals/ chapter/data-types/. Accessed 07 Oct. 2023.

Python Data Types, www.w3schools.com/python/python_datatypes.asp. Accessed 07 Oct. 2023.