Basic Data Types and Casting in Python

Python is an interpreted, high-level, dynamic typed, general-purpose programming language. Python helps programmers write clear, logical code for projects of all sizes by offering object-oriented approach efficient language constructs.

Like most programming languages, Python supports common data types:

  • Float,
  • Integer
  • String
  • Boolean

These four makeup the most common data types built into Python. Of course there are many other supported data types in the Python language but for the purpose of this article our focus will be on these four.

Declaring variables in Python

Creating a variable in Python is incredibly easy. If I wanted to save a users age, as an integer, I could simply write:

user_age = 25

You might notice that I didn’t place a semicolon following the line. That is because Python doesn’t use semicolons to indicate the end of a statement. For this reason, it is immensely important to be vigilant in your white space and tabs which Python does use to indicate the beginning and end of a statement, along with the logic contained within.

Capitalization Matters

In Python, as with most programming languages in mainstream use today, capitalization matters.

user_age = 25 is not the same as: user_Age = 25

The capitalized ‘A’ tells the interpreter that our second variable is a different one than the first.

Performing Math on Strings

Python also allow us to perform a few operations on strings that can be confusing at first. For example:

my_var = "5"
other_var = my_var * 4
print(other_var)

would produce the output: 5555. This is a string even though it printed 5555. This can be slightly confusing if you are coming from another language such as C# where:

String my_var = "5";
String other_var = my_var * 4;
Console.Write(other_var);

would definitely would not build. Instead, you would see the error on hovering the red underlining of the lines “Operator ‘*’ cannot be applied to operands of type ‘string’ and ‘int'” Maybe you didn’t intend to multiply a string though. Even though Python doesn’t have explicitly declared types we can still cast to ensure the proper data type is enforced.

Casting

To perform math on two integers we would simply change our code to say:

my_var = "5"
other_var = int(my_var) * 4
print(other_var)

and our output would now reflect: 16. The Python interpreter knows that although we initialized [my_var] as a string, that we want the operation with [other_var] to be treated as multiplication on two integers.

I could also cast to float simply by writing:

my_var = "5"
other_var = float(my_var) / 3
print(other_var)

and then we would see our output of : 1.3333333333333333.

Last, if we wanted to make use of our Boolean data type we might then check if [my_var] is equal to [other_var] by writing:

my_var = "4"
other_var = float(my_var) / 3
print(other_var == my_var)

our output would be: False.

Casting to Boolean

The last of our four can be a little confusing as well if you are not familiar with it. In using Boolean, I would suggest that you keep it to validation and avoid attempting to cast. This is because empty strings evaluate always to False, everything else evaluates to True. For this reason Boolean, should not be used for any kind of parsing purposes. To see this you could write:

print(bool("Patrick")) which would output: True
print(bool("")) which would output: False

Type Checking in Python

As I stated above, Python is a dynamically typed programming language. This means that data types are enforced at run time in Python, as opposed to compile time in languages like: Java or C#. This also means that I may write code that appears okay while I am writing, such as:

my_var = "Patrick"
other_var = int(my_var)
print(other_var)

and then when I go to run my program I see:

ValueError: invalid literal for int() with base 10: 'Patrick'

At run time, the Python interpreter caught the type issue and alerted me. This is a core difference between dynamically and statically typed programming languages. Of course, I should note that the definition of what makes a language dynamic or static is not black and white and a lot of gray area exists in real world application, more on that later.

I hope this chunk of information was easy enough to consume.

Thanks for visiting.