Python Tutorial #1: Working with Different Data Types

No matter what programming language you use, the type of you data will be a very important factor in how you code. Because of this, there are certain built-in attributes in Python that are data type specific. Below we’re going to look at some example programs that work with different data types, and see what we can do with them. If you need a refresher on data types and their differences, click here.

Part 1: Working with Strings

A string is a collection of characters (numbers, letters and other symbols).

The Sample Code:

  1. # Part One: Working with Strings
  2. str = “The Blind Programmer”
  3.  
  4. print(len(str))
  5. input(“Hit Enter to Continue:”)
  6.  
  7. str_1 = “The Blind “
  8. str_2 = “Programmer”
  9.  
  10. print(str_1 + str_2)
  11. input(“Hit Enter to Continue:”)

The first print statement is interesting because it is actually taking another function as an input. When you pass a string to the len() function it returns how many characters are in it. In this case, it returns the number 20.

In lines 7 and 8, two strings are defined in the variables str_1 and str_2. Therefore, line 10 is an example of string concatenation. That means we are combining the two strings into one. The output in this case is “The Blind Programmer”.

Part 2: Working with Integers

An integer is a whole number. This number can be negative or positive, and can also be 0.

The Sample Code:

  1. #Part Two: Working with Integers
  2. num_1 = 7
  3. num_2 = 5
  4.  
  5. print(num_1 + num_2)
  6. input(“Hit Enter to Continue:”)
  7.  
  8. num_1 = 10
  9. num_2 = num_1 / 4
  10.  
  11. num_3 = num_1 // 4
  12.  
  13. print(num_2)
  14. print(num_3)
  15. input(“Hit Enter to Continue:”)

The first part of the code is a simple addition problem, the output is 5 + 7 = 12. Notice that when our inputs were strings, the + operator simply printed one variable after the other. So the + is an example of an operator in Python that works differently when used on different data types.

The next part of the code shows the difference between regular division and integer division. Integer division is a python feature that allows us to immediately chop off the remainder of a division problem that doesn’t return a whole number. On the other hand, regular division in python will sometimes return a floating point number or float (a number with a decimal point). In this example, num_2 is a float, 2.5, and num_3 is an integer, 2.

Part 3: Working with Strings and Integers

In this part we will work with both strings and integers.

The Sample Code:

  1. # Part 3: Working with Strings and Integers
  2. str = “TheBlindProgrammer”
  3. num = 2
  4.  
  5. print(str * num)
  6. input(“Hit Enter to continue:”)
  7.  
  8. str = “The Number is “
  9. num = 5
  10.  
  11. print(str + num)
  12. input(“Hit Enter to continue:”)

As you can see in line 5, the multiplication operator (*) in python can actually work on more than one data type at a time. That being said, you should be careful with the order you put the data in when using this method. Line 5 of the program will print “TheBlindProgrammerTheBlindProgrammer”, but only when the integer is placed after the string.

In the next part, the program will actually crash when it tries to complete line 11. This is because the print() function cannot work with more than one data type at a time. To fix it, you would have to convert num to a string using the string() function. The line would now look like this:

11. print(str + string(num))

You should try some more experiments like this using other data types. Getting a good grasp on how different data types are used is really important. Feel free to comment on this post if you have any questions, and I’ll see you in the next one.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.