Tutorial

Primitive Types in Python: Variables (Numbers, Booleans and Strings)

Variables

Let’s start this section by discussing variables which are one of the Core Concepts in programming. We use variables to store data in computer’s memory.

Here are a few examples.

students_count = 1000

Above, I defined a variable called students_count and setting it to 1000. When we run this program, python interpreter will allocate some memory and store the number, 1000, in that memory of space. Then it will have the variable, students_count reference that memory location.

So the variable, students_count, is just like a label for that memory location. We can use the variable anywhere in our program to get access to that memory location and the data stored there. So now if we print students_count and run our program, we will get 1000.

setting variable student count

So this is the basic of variables. Now what kind of data can we tore in a computer’s memory? Well, We have several different kinds of data, in this section, we’re going to look at the built-in primitive types in Python.

Python Primitive Types

Primitive types can be numbers, booleans and strings. Let me show you

  1. students_count = 1000 Number
  2. is_published = False Boolean
  3. course_name = “Python Programming”String

Number

So the first on the list here, we have a whole number we refer to this as an integer in programming. We can also have numbers with a decimal point for example 4.99. This is what we call a float or floating Point number. This terminology is not specific to python alone. In the future when you learn a new programming language, you’re going to hear these terms again.

Boolean

Now on the second example able, we have a Boolean. Here we’re asking if a publication is published, if it is, we set this to True or False. These are examples of Boolean values in programming. So Boolean values can either be True or False and these are exactly like saying, Yes and No in English.

Later, you will learn that we use this Boolean values to make decisions in our programs. For example, if the user is an admin user, perhaps we want to give them extra permissions.

So take into account that python is a case sensitive language, which means lower case and upper case characters have different meanings. So Boolean values should always start with a capital letter like what you see above. If we type false or FALSE, these are not accepted Boolean values in Python. Only what you see above, False or True, is a valid Boolean value.

Strings

Finally, let’s take a look at the example of a string.

course_name = “Python Programming”String

So above, we set course_name to the string, Python Programming. So string as I told you before is like text. Whenever you want to work with text in your programs, you need to surround it with quotes.

So these are the basics of variables.

4 things I’ve consistently used in my variables?

1. The first thing is that all my variable names are descriptive and meaningful. So students_count represents the number of students for a course, course_name clearly explains that this variable holds the name of a course.

One of the issues that I see a lot amongst beginner programmers is that they use mystical names for their variables something like this cn as a short for course name When someone reads this code, they have no idea what cn stands for, or they use variable names like c1. When I look at that code, I wonder where is c2 and what is the difference between c1 and c2.

So these variable names are very mystical and that’s a bad practice. Make sure your variable names are always descriptive and meaningful because this makes your code more maintainable.

Now, there are times that you can use short variable names like x y z if you’re dealing with Things like coordinates. So that’s an exception.

2. The second thing that I have consistently used in this code is that I have used lowercase letters to name my variables. So here we don’t have COURSE_NAME or Course_Name. All letters must be lowercase.

3. The third thing that I’ve consistently used here is that I have used an underscore to separate multiple words and I’ve done this to make my variable names more readable because in Python, we cannot have a space in variable names, so we cannot have “course name” and if you put these two words together, it’s a little bit hard to read “coursename“. That’s why we use an underscore.

4. The fourth thing that I have used consistently here is that I have put a space around this equal sign again, that’s one of the issues I see a lot amongst beginners. They write code like this:

course_name=“Python Programming” – without a space around this equal sign.

This is a little bit ugly. This is what we call Dirty code. You should write code that is clean and beautiful so other people can read it like a story, like a newspaper article. It should be formatted properly and that’s why we have pep 8 in Python. Now the good thing is if you forget these rules when you save the changes auto pep 8 kicks in and it automatically reformat your code, but that aside you should always give yourself the habit of writing clean code without relying too much on the tooling.

So these are all the best practices about naming your variables next. We’re going to look at strings in more detail.

Strings

course = “Python Programming”

So here we have this course variable set to “Python Programming” as I told you before whenever you work with text, you should surround your text with quotes. You can either use double quotes(“) or single quotes(‘). That’s more of a personal preference but quite often we use double quotes(“).

We also have triple quotes(“””) and we use them to format a long string. For example,

message = “””
Hi Michael,

This is your friend from school,
We have exams next week.
“””

If you have a variable, message, that is the message we want to include in the body of an Email, you can use triple quotes to format it like the one above. So that’s when we use triple quotes.

Useful things you can do with strings.

First of all, we have this built-in function in Python for getting the length of strings. Let me quickly explain what a function is. A function is basically a reusable piece of code that carries out a task.

As a metaphor, think of the remote control of your TV on this remote control you have buttons for different functions, like turn on, turn off, change the channel and so on. These are the built-in functions in your TV, in Python and many other programming languages, we have the exact same concept.

So we have functions that are built into the language on the platform. You can reuse this functions to perform various task.

Example:

course = “Python Programming”
print(len(course))

so here we can use the built-in len function to get the length of a string, which means the number of characters in that string. Now whenever you want to use a function, you should use parenthesis. That is calling the function which basically is like using the function.

Now some functions take additional data like the example above, which we refer to as arguments. These arguments are input for this functions.

We get 18 when we execute the example code above. Meaning we have 18 characters here.

Let’s look at another example: If you want to get access to a specific character in the string you use the square bracket notation.

corse[0] – to get the first character you use the index zero. So in Python, like many other languages strings are zero indexed which means the index of the first character or the first element is 0. So now when you print the above code, we get P.

corse[-1] Now you can also use a negative index like -1. what does that mean? Well, if 0 represents the first character, -1 takes us back to the end of the string so that Returns the first character from the end of the string. Run the program and you will get G.

Using a similar syntax, you can slice strings. Let’s say we want to extract the first three characters in this string.

corse[0:3]

This will return a new string that contains the first three characters in this course variable. That will be P Y and T. So the index of these characters are 0 1 and 2. So that means the character at the end index is not included.

Python Escape Sequence

We have this string here Python Programming.
course = “Python Programming”

Let’s say we want to put a double quote in the middle of the string.

course = “Python “Programming”

There is a problem here, python interpreter sees the second quote as the end of the string. So the rest of the code is meaningless and invalid. How do we solve this problem? Well, there are two ways.

One way is to use single quotes. Double quote in the middle of the string, but what if for whatever reason perhaps for being consistent in our code we decided to use double quotes. How can we add another double quote in the middle of the string?

Well, we can prefix it with a backslash \. backslash in Python strings is a special character. We have a jargon for that called Escape character. We use it to escape the character after. So backslash, \, is an escape character and backslash double quote, \”, is an escape sequence.

In Python strings we have a few other Escape sequences that you should be aware of. Let me show you.

In Python, we use # sign to indicate a comment. A comment is like additional note that we add to our program. It’s not executed by python interpreter.

We also have double backslash. So if you want to include a backslash in your strings, you should prefix it with another backslash.

And finally, we have \n which is short for new line. So program continues on the second line. So these are the Escape sequences in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *