• 6 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 1/21/21

Understand numeric and string data types

Log in or subscribe for free to enjoy all this course has to offer!

Why do we need data types?

We explored a little bit about data types in the last chapter, but there's so much more to know - like primitive data types!

Primitive data types are those that exist on their own - like atoms. They are the simplest types and are the foundation for all computer operations. You can combine them to construct more complex types, as you will see in the next chapter. You'll use numeric types and strings most often. Let's check them out!

Numeric types

These are:

  • Integers, which are whole numbers, like the numbers you use to count (1, 2, 3).

  • Decimals, like those you may use to store money values (2.50 or 5.99).

Let's start with one you've gotten a little familiar with: integers! Integers are declared like any other variable, with the name then the value: 

count = 10

As you can see, your variable's value is 10, which is, of course, an integer: therefore, your variable count has an integer type. 😉  

For decimals, Python uses float type. You can define it in the same way as integers:

length = 1876.79
width = 1876.79797657

As long as the value is a decimal number, Python will automatically store it as a float variable.

Mixing numeric types

An important thing to keep in mind is how the types mix together and what consequences it may have. If you mix together different types, the higher will always apply to the result. For example, an integer can be a float, but the opposite is not necessarily true!! A float is the higher type: if you mix a float with an integer, the result will always be a float no matter the operation or the result.

Let's review few examples in Python:

a = 7.5
b = 3
c = a/b
c
# that will display 2.5, which is a float

If the result of the operation between two integers is supposed to be a decimal number, Python will convert it to a float:

a = 10
b = 4
c = a/b
print(c)
# c is a float

However, you can convert the type of an object to another. This is also called typecasting because the user casts (changes) the type of the objects. For that, you have to use the corresponding functions:

  • int() : for integers

  • float() : for floats

a = 14.0
# a is a float
a = int(a)
# a is now an integer, so it would display 14 and not 14.0
Try it out for yourself!

Coding application
Houston... ?
It seems you're not connected to the internet.
You are not signed in

Sign in to access coding exercises and test your new skills.

Make sure to practice in this chapter before moving on.

Strings

Next, we are going to check out a bit more sophisticated type: string. Strings allow for storing text, or in other words, a set of characters. Here's how you declare a string variable in Python:

city = "New York"
movie = 'Best ever'
emptyString = ""

Merging one or more strings together is one of the more common actions you can perform. Let's join some strings together:

firstFavoriteCity = "New York"
secondFavoriteCity = "Buenos Aires"
favorites = firstFavoriteCity + secondFavoriteCity # -> "New YorkBuenos Aires"

Notice that you don't have a space in between. Let's make it more readable by concatenating strings and variables:

firstFavoriteCity = "New York"
secondFavoriteCity = "Buenos Aires"
favorites = "My favorite cities are " +firstFavoriteCity+ " and "+secondFavoriteCity # -> "My favorite cities are New York and Buenos Aires"

Much better now! You can't concatenate other data types with strings.; however, you can use typecasting by using the str() function. How do you do that ?

favoriteCity = "Buenos Aires"
numberOfTrips = 5
story = "I've traveled to " +favoriteCity+ " " +str(numberOfTrips)+ " times!" # -> "I've traveled to Buenos Aires 5 times!"

In the last section, we used the + operator to add two numbers. The + operator can also be used to concatenate (join) strings together. 

Try it out for yourself!

 

Coding application
Houston... ?
It seems you're not connected to the internet.
You are not signed in

Sign in to access coding exercises and test your new skills.

Make sure to practice in this chapter before moving on.

Regulations

Remember, variables always have a type, and Python doesn't require them to be specified when you declare a variable. If you need to use the content of a variable as if it was of another type, you can use typecasting.

Summary

In this chapter, you've learned the specifics of a few simple data types:

  • Integers

  • Decimals

  • Strings

You've also learned how to manipulate these types:

  • You can perform numeric operations on numbers of the same type.

  • To use different types of numbers together in operations, cast them to behave according to your type needs. 

  • Strings can be joined together. This is called concatenation. 

Example of certificate of achievement
Example of certificate of achievement