Learn about Python 3 data types — numbers and strings

Shawn Ren
3 min readOct 11, 2019

--

I recently started to learn python 3 from Udemy. Since I already have learned ruby and javascript, learning python is more like trying to find out the differences and the similarities. Let’s start from the basic, data types.

Python data types

Here are eight types of data in the above table. Based on what I have learned from ruby and javascript, there is no array and object or hash in it. However, we can see there is the list which is similar to an array and dictionary which is similar to hash or object. Let’s break this table down to see what the differences and similarities.

Integers and Floating Point

In python, integers and numbers with decimal are two different data types. But in javascript, there is only one data type “number” to combine them.

Two different data types in Python
Data type of number in javascript

Strings

Both languages have ‘strings’ data type, and the syntax are same, you can either use single quotes or double quotes. Both are ordered sequences, which means we can use [] square brackets and a number index to indicate position of the character we want to grab out of the string. There is a difference here between two languages. In python, there is a reverse index of each character, while in javascript, there is no such thing. You can see from the below example:

“hello”[-2] undefined in js vs “l” in python

Another cool function in python is the default slice method. Remember when I learnt javascript, if I want to find some substring of a string I need to call .slice(start, end) on the string. But in python, I can just use [start:end:step] square brackets to find the substring, I can even take two steps to call the substring every other character of the original one. There is a trick in python: remember the classic interview question about reverse string, instead of using a for loop, in python you can just call [::-1]behind the string. Isn’t it neat?

Call substring

Another thing is that if you try to run 2 + ‘3’, there will be two different results: for python, it will say type error, because you can’t use integer plus a string; while in javascript, it will return ‘23’, the same result of ‘2’ + ‘3’. And also there is a split() method of the string which python could use to separate the string based on the separator. But remember when we were in javascript, if we want to totally split the string to make each character to be an element of the new array, we just call .split(‘’) at the end of the string? Here in python, if we do that, it will return a value error: empty separator. Because in python, the split method is expecting some separator between quotes. So how could we achieve that action in python? Dose python doesn’t have that feature? Or do we have to use a loop to make the array? Here is actually a very easy way to do so, just run list(mystring) which I will learn the main differences of list, dictionary and array, object later. Stay tuned and thanks for reading!

--

--