We are getting closer to the finish line of Module Two in the SoloLearn course, Introduction to Python. The next topic in this module will focus on data types and data type conversion. In today's post, you will be able to identify different data types and be able to convert them into different ones you need as you continue to code in Python.
Data Types
Computers work with different types of data, so you must be familiar with them all. You are already familiar with the first type the string. Strings are the data type for text.
Strings are always wrapped in quotes. They can be wrapped in single quotes or double quotes.
print("Hello World")
print('Lucky')
The example above is printing two strings. These are strings because they are wrapped in quotes. The first line is printing a string with double quotes while the second line prints one in single quotes.
One of the things you can do with strings is concatenate them together. Concatenate means that strings can be joined together. To concatenate a string, you put a plus sign between two strings. Sometimes developers will even concatenate spaces in between strings.
print("Hello" + " World") # print Hello World to console
name = "Lucky"
print("Hello " + name) # print Hello Lucky to console
In this sample code, the code is printing two different strings. The first line of code is concatenating "Hello" and "World" together. Before "World" I put a space before the W. This will make sure "Hello" and "World" have a space between them or else you will see "HelloWorld" in the console.
The second string creates a name variable with a string assigned to it. Inside the print function, I will print "Hello" followed by the name variable. When the computer runs this code, the name variable will be replaced with the value assigned to it.
Be careful with your spacing!
Concatenating strings together means you will need to pay attention to the spacing. Sometimes you might be able to put a space before or after certain words. Other times you will need to put an actual space in between strings using the quotes.
print("Hello" + " " + "World") # prints Hello World to the console
In this example, I'm using quotes to create a third string between "Hello" and "World". This string is just an empty space. When the computer runs this code, it will create the space.
The computer doesn't realize there are spaces between words so you need to be specific if you want a space between two strings. If you don't, the computer will just combine the text together as if it is one word.
first_name = “Lucky”
last_name = “Dye”
print(“Hi, my name is” + first_name + last_name) # will print Hi, my name isLuckyDye to the console
Don't forget that punctuation makes a difference in text-based languages like Python. So make sure you double-check that your strings are using opening and closing quotes. Consistency is key with punctuation so if you are using single quotes, you will want to use single quotes for your string. Don't mix single and double quotes together.
Integers
You have only been using strings, but today you will start learning brand new data types. Up first is the integer.
Integers are the whole numbers that don't have decimal points. Integers can be zero, positive, or negative.
print(2025) # will print 2025
print(4) # print 4
print(-3) # print -3
print(0) # print 0
The code sample above prints different integers to the console. There are positive integers, negative integers, and zero. Notice that each integer doesn't need any quotes wrapped around them.
If you see a number wrapped in quotes, that isn't an integer. Numbers that wrapped in quotes are strings. This means the computer will see these numbers as text.
Integers wrapped in quotes will not make it possible for you to do any calculations. When developers do any calculations, they do these first then turn the result of the calculations into strings.
print("3") # print 3 as a string
Floats
The next data type you need to know in Python are floats. Floats are numbers that have decimals. Although these numbers are different from integers, the one similarity between these data types is that they use positive, negative, and zero numbers.
print(3.14) # print 3.14
print(-33.3) # print -33.3
print(0.28) # print 0.28
This code sample prints different floats to the console. Each line shows positive and negative numbers.
When you start doing calculations, dividing two integers together will result in a float. Remember when floats are wrapped in quotes, that means they are strings. So that means you won't be able to do any calculations.
The type() function
Now you know 3 out of 4 data types. This means you can check what data type you are using. Python has a built-in function you can use to figure out what data type is being used.
Let me introduce you to the type() function. This function helps developers check what data type is being used. This is an important function developers can use because data comes in all different types.
Sometimes the data developers receive won't match what they need. To help developers avoid using the wrong data format in their web apps and games, the type() function helps developers check what format the data is in as they receive it.
print(type("Lucky")) # print string
print(type(18)) # print integer
print(type(10.25)) # print float
This code sample prints the types of data. To use the type function, you put the type keyword followed by parentheses. Inside the parentheses is the data you want to know the type of.
Errors happen whenever you use the wrong format. Computers act differently for each data type and is one of the quickest ways to get an error message if you aren't careful. So use the type function when you get any data for a project before you start coding.
Implicit and Explicit conversions
Before we can start talking about data conversions, SoloLearn takes a minute to talk about explicit and implicit conversations. Implicit conversions are automatic conversions. Explicit conversions are not.
X = 5 # integers
Y = 2 # integer
Z = x/y # float
print(z)
The above code sample is an example of implicit conversion. The computer has two integers assigned to variables. In the variable z, the computer divides x and y to create a float.
The reason explicit conversations are not automatic is that the computer needs the developer to tell it the instructions that need to be done. When developers convert data into different data types, the functions they use are examples of explicit conversion because developers are able to change values this way. Three functions that developers use to create explicit conversion are int(), str(), and float().
Developers want to avoid making more work for themselves, and using data in the wrong format will do this as well as impact your code. These three functions are tools we can use to make sure everything is in the right format so we avoid bugs later down the road. However, as you continue with Python you will need both explicit and implicit conversions so it is key that you know both.
Data Conversions
If you receive data that is the wrong data type, Python makes it possible for developers to easily data into the right data type. This is where the explicit functions int(), float(), and str() can be used.
The int() function
The int() function is how you can turn a string into an integer. To use this function, put the string inside the parentheses after the int keyword.
answer = int(input(“Pick a number between 1 and 5”))
print(type(answer)) # prints integer to screen
In this example, the computer asks the user for a number. When the user gives a number, that response is saved to the answer variable. If there were no int() function, the computer would treat the response as a string.
You will often see the int() function used with other functions such as the input() function. When developers want an input from a user, they will use the int() function around the answer given to make sure it is a number before they start using calculations.
The float() function
Do you need a float? You can turn integer into a float using the float() function. When you put an integer inside the parentheses of the float function, the integer will be transformed into a decimal.
grade = float(88)
print(type(grade)) # print float
This code sample has a variable named grade assigned a value of float(88). The float() function will turn 88 into a float when the computer runs this code. The second line prints what type of data the grade variable is in the console.
The str() function
If you need a string? You can turn integers or floats into a string using the str() function. At Coding with Kids, students use the str() function to turn integers and floats into strings for messages in their games.
To use the str() function, put the integer or float in the parentheses of the str() function. When the computer runs the code, the computer will turn the number into text.
height = 34.8
width = 52
print(str(height)) # print 34.8 as a string
print(str(width)) # print 52 as a string
This example has two variables. The height variabble has a float assigned to it while the width variable has an integer. The computer prints both variables as string using the str() function.
Top comments (0)