What is Python part 2
Tags: python, computerscience
Published: 2020-09-20 22:16:43

Hello! Last week I gave a brief introduction to the world of Python and this week I'm going to dive in a little deeper!

First I'm going to touch on the different data types found in Python and then focus on the Number and String data types. You can find the data type of any element by using the type() command. To see the data type printed out you would run this command in conjunction with the print command.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t4aly3q0pjgoevzne599.png)

Here I set a variable to contain a string value. When I run the file in the terminal, this is what get's printed out.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jlorwgqpatx4n4qfrfyq.png)

Python has several different data type categories and some categories have different data types within the category. [W3Schools tells us:](https://www.w3schools.com/python/python_datatypes.asp)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/33hs0hj28h4wr04cx987.png)

The data type is set whenever a value is assigned to any variable and you can change the type by assigning a different type to that variable.

The Number data type category consists of three different types: int, float, and complex. Int is short for integer and represents a positive or negative whole number and the length can be unlimited. Here is an example:  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j52uh0t91btx5bmn4l61.png)

Now when I run the file in the terminal I get 'int' type back:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mxwj9tee49hlo312ps45.png)

The 'float' Number type consists of a positive or negative number containing at least one decimal.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2n1nnuvpyry12pamb6vi.png)

Now I get type 'float' back:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6gkd6xelfh2rwn2jrr4x.png)

Another common data type found in Python and other languages is the String data type. A string can be formed by wrapping it in either single quotes or double-quotes. To form a multi-line string, three quotes are used and the line break output will correspond to the line breaks found in the code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2bev4eul39vlun9moy1o.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1vpvnj7669u8ixchjdcy.png)

In Python, strings are considered arrays and each character is a single string. Similar to JavaScript, you can access a certain character in a string by wrapping the index number in square brackets and preceding that with the variable name.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d7dodbdos9nnjlcff8y0.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b0swons64e5t3brfb0it.png)

You can find the length of a string by using the len() function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b5gngf56u7gek9058c2z.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cnt3ihd57ojjgizs08f5.png)

Python also has a slice method which will return a section of a string back. To implement this method, you need to declare the string to be sliced and then wrap the starting index and stopping index in square brackets and these numbers need to be separated by a colon. The last index value will not be included in the return value.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/whmb73y7vk73x6kwxiyp.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f9gt3q70htofqwcms2qx.png)

Negative slicing can also be implemented by using negative numbers. When this is done, the slice starts at the end of the string.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s4c2u6jr81no8k1267zd.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h0pv7wc17gm4l2c31ont.png)

This has been a brief introduction to some of the different data types of Python. Python uses a lot of the same data types as other programming languages, like Number and String, with some subtle differences. Thanks for reading!