Getting Started with Python Programming (Part 2): Variables and Basic Data Types

Last time, we slithered through memory lane and looked at a brief history of the Python programming language and successfully got ‘Hello World’ printed on the screen using the print() function. If the above sounds strange to you, you might want to check out the first part of this tutorial series here.

To better understand this tutorial and programming in general, it is good to experiment, change and alter stuff to see the program’s response to it and practice, practice, practice. You may not be world-class after all that practice, but you definitely will be a much better programmer. So, practice!!!!

In this part, we’ll be looking at Variables and the Basic Data Types that are available in Python – the cool stuff (not quite, lol).

Variables

Variables are like placeholders for data. Imagine a cup that holds hot coffee. The cup is the variable, while the coffee is the data placed in the variable.

In Python, variables are declared by writing the variable name and assigning it a value using the assignment operator ‘=’ (more on operators later).

NOTE: The ‘>>>’ shows that the code is being run in the Python terminal.

The above code does the same thing as the code from the previous part, only this time, the message is first stored in a variable. This is good for us because this variable can be referenced again and again to your heart’s content as will be seen later.

Try changing the content of the message variable and write more variables of your own – a variable to hold your crush’s name for example (who knows? Might get you a yes. *wink).

Strings

You’ve already worked with strings before now. The ‘Hello World’ message and hopefully your crush’s name are examples of strings. Strings are by definition a continuous series of characters. They are placed in quotes (single or double quotes work in Python), that is, ‘Hello World’ and “Hello World” are both valid strings. This is important as you may need to use quotes and apostrophes in a desired string. Strings like, “Valar Morghulis’, said Arya” and “Master Yoda’s lightsaber is colored green” would be impossible to write without the single and double quotes present in Python.

There are a few ways to work with strings in Python. These are called string methods.

String Methods
Changing Case

These methods allow you to render any given string in the given method case. They are the title(), upper() and lower() methods. For example:

Concatenation

Concatenation deals with joining one or more strings together. It is achieved using the ‘+’ operator. For example:

NOTE: I added a space after “Hello” because, without it, the string will be jumbled up like this, “HelloWorld.” Python sees whitespace in strings as characters.

Numbers

Numbers in Python are two basic types – integers and floats.

Integers

Integers are whole numbers. Numbers without decimal points (2, 3, 909209304903940) are examples of valid integers in Python. You can perform regular arithmetic operations on integers – addition, subtraction, multiplication, division, exponential, and also the modulo operation (%) used to find the remainder from a given division.

Floats

Python sees every number that has a decimal point as a float. You can perform the same operations with floats as with integers. You can also round up a float to a given number of decimal places with the Python in-built round() function. You do this by passing the number and the number of places (functions and arguments will be discussed later).

So, that’s it for this second part of the tutorial. Go through the tutorial multiples or contact me if you do not understand me, and, most importantly: Practice, Practice, Practice!

Related Posts