Tag Archives: python programming

Common Data Types in Python

All programming languages have a way of storing certain types of information in variables. Certain data types or needed for one situation and different data types for another. It is important to know the differences in the data types otherwise serious problems could arise when developing an application. In this post, we will look at some of the more commonly used data types in Python.

Making Variables

It is first important to understand how to make a variable in Python. It is not that complicated. The format is the following

variable name =  data inside the variable

You simply type a name, use the equal sign, and then include the data to be saved in the variable. Below is an example where I save the number 3 inside a variable called “example”

example=3
print(example)
3

The “print” function was used to display the contents of the “example” variable.

Numeric Types

There are two commonly used numeric data types in Python and they are integers and floating point values.

Integers

Integers are simply whole positive or negative numbers. To specifically save a number as an integer you place the number inside the “int” before saving as a variable as in the example below.

example=int(3)

print(example)
3

You can check the data type by using the “type” function on your variable. This is shown below.

type(example)
Out[17]: int

The results are “int” which stands for integer.

Floating-Point Types

Floating-point numbers are numbers with decimals. If your number includes a decimal it will automatically be stored as a floating type. If your number is a whole number and you want to save it as a floating type you need to use the “float” function when storing the data. Below are examples of both

#This is an example of a float number

example=3.23

print(example)
3.23

#This is an example of converting a whole number to a floating point

example=float(3)

print(example)
3.0

Floating points can store exponent numbers using scientific notation. Floating point numbers are used because decimals are part of the real world. The downside is they use a lot of memory compared to integers.

Other Types

We will look at two additional data types and they are boolean and string.

Boolean

A boolean variable only has two possible values which are True or False. This seems useless but it is powerful when it is time to have your application do things based on conditions. You are not really limited to True or False you can also type in mathematical expressions that Python evaluates. Below are some examples.

#Variable set to True

example=True

print(example)
True

#Variable set to True after evaluting an expression

example=1<2

print(example)
True

String

A string is a variable that contains text. The text is always enclosed in quotations and can be numbers, text, or a combination of both.

example="ERT is an awesome blog"

print(example)
ERT is an awesome blog

Conclusion

Programming is essentially about rearranging data for various purposes. Therefore, it only makes sense that there would be different ways to store data. This post provides some common forms in which data can manifest itself while using Python.

Advertisements

Intro to Python

Python is a highly popular programming language. It is so popular that it is now the most commonly used programming language for machine learning/data science purposes having surpassed R.

However, Python is not limited to just statistical tools. Python is also used by many companies for a host of reasons including Yahoo, Dropbox,  Google, NASA, IBM, and Mozilla.

One secret to Pythons popular is its flexibility. When using Python it is possible to employ several different coding styles. Below is just some of them.

  • Procedural: This is the simplest form of coding and involves executing each line of code sequential.
  • Functional: Functions are used to transform data as found in mathematics.
  • Imperative: Employs statements to achieve a goal
  • Object-oriented: The use of objects (aka data structures) to model the real world. Not fully implemented in Python.

You can mix these styles together to make powerful applications.

Using Python

You can download Python by searching for “Anaconda Python” in Google. The Anaconda version of Python downloads several additional features to besides Python including the Spyder IDE which is what we will use here.

Once you download and install Anaconda, on your computer you need to search for the program called “SPyder”. When you open it you will see the following.

1.png

Here is what each pane represents.

  • To the left is the text editor, you can type code that you want to save here.
  • In the top right is the variable explorer. Here you can find a list of the objects you have made.
  • In the bottom right is the Interactive Python console or “IPython” console for short. Here you can type code quickly without the need of storing it for long-term use. In addition, the results of any code execution is normally displayed here as well

When writing code remember that you can save it long term in the text editor or just execute it quickly in  the console,

First Line of Code

We will now run our first line of code. Followed by the output in the IPython Console. Below is what we typed into the Console

print("Hello to Python")

Here is what it looks like in the console

1

Here is what we did.

  1. We typed “print(“Hello to Python”)” into the console. This is an example of the use of a function.
  2. The output provides several pieces of information.
    • The blue shows what line this is in the console. In other words, this is the third line of code I had typed in the console. You may have a different number.
    • The purple is the function being used which for us is the “print” function which simply displays the input
    • The green is the argument that the function is changing. Our argument is a string of text that is put in quotes deliberately
    • The text in black is the actual output

Of course, there is much more to Python then this. However, this serves as an introduction for a future post.

Conclusion

Python is a popular programming language used in a variety of application. The source of its popularity has to do with it general-purpose philosophy. There’s a little bit of something for everybody in this language which encourages its use. Using the Spyder IDE will allow you to experience Python for the purpose of acquiring new skills.