Learning Python – Variables & Types

Introduction

Python is a strange language to me, but very popular. There are no curly braces or anything when declaring functions if you’ve come from JavaScript, C#, Java, etc. Python uses whitespace to define the structure of programs. There are no semicolons used in the language, unless absolutely necessary. But, it’s a great language with a lot of uses as a scripting language. Let’s talk about the basics of Python: variables and types.

What Are Variables?

Variables in any programming language are storage places for values that provide them with a unique identifier. In short, they give a value a unique name. So, what’s special about Python variables? Here’s an example of a Python variable:

Python variables are dynamic; that means they can be changed at run-time(when the program is running). This means a variable could be equal to some text, then a number, and so on. This might be familiar to you if you come from JavaScript, So, this brings the next question, what types are the values?

What Are Types?

Types are something familiar to people who come from statically typed languages(C, C++, C#), over dynamic languages (JavaScript). Types inform the programmer and the computer what the value should be considered and interpreted as. For example, a variable containing the value 2 should be capable of being added to another number. Now, a string of text, shouldn’t be able to be added to a number. Here’s an example of the concept:

 

As you can see, we can only add two compatible types together, because types help the computer understands what operations we can apply to what variables or values. On the last line of the example, we had to convert the type of mynumber2, in order to add our string of text to our number.That’s why types are important to programmers; to define what object type can perform which operations. Python’s main types are number, string, list(an array), dictionary( an object with key and value pairs), and object. Each type has its own operations and one important thing to note is that each type is a specialized object that inherits from the object type. This is the core concept of an object oriented programming language and should be remembered.

I hope this introduction to variables and types was helpful to Python variables, and if you have your own interpretation of the same information please share it below; this is only my understanding of Python variables. Understanding different types is an important part of understanding a programming language and creating your own types in your own programs!

Leave a Reply

Your email address will not be published.