Chapter 2. Variables, Functions, and Users

In the previous chapter, you learned how to install Python on your computer. You also learned how to use the print statement in Python and printed some messages using your Python shell. We are now going to jump into a lot of details so that we can build our first project together. It will include the following:

  • Variables
  • Variable names
  • Strings, integers, and floats
  • Functions

Variables

A variable is when one letter or word is used to represent a different letter, word, number, or value. One way to think of a variable is to imagine that you are programming a computer so that it can make memories. For example, my name is Jessica. If I am writing a computer program and I want that program to remember my name, I will assign my name to a variable. This will look like name = 'Jessica'. The variable is name. Jessica in the memory.

Perhaps I would like the computer to remember something else about me as well. Maybe I want the computer program to remember that I am 64 inches, or roughly 128 cm, tall. I will say height_inches = 64 or height_centimeters = 128. The variables here are height_inches and height_centimeters. The memories are my height in inches and my height in centimeters.

Why don't you try giving a computer the name variable with your name and then a height variable with your height?

First, open your Python shell and type the following code:

name = 'yourname'
height = 'your height'

Now that the variables are stored, you can type print(name) or print(height). Since you created a memory for the computer with your name and your height, the computer will print the memory that you gave it. If you take a look at the screenshot from my Python shell, you will see that the computer printed the memories that I assigned it. Notice that we do not use single quotes around the variable names:

Variables

If the values, or memories, that you assigned to the variables are printed in your Python terminal, then it is perfect. If not, you may have an error. There are a lot of reasons due to which an error can occur. You may have typed your variable name or your information in a way that breaks the Python convention. Some common errors include using capital letters.

Naming variables – conventions to follow

There are some conventions that are used to name variables in Python. It might seem silly to have guidelines about naming things, but following the conventions is really important because it helps other people read your code. Also, the Python shell is designed to work with the conventions.

To avoid errors, your variable names should use lowercase letters. If your variable uses more than one word, such as the height_inches variable, then it should have underscores to connect the words together.

If you use two words to name your variable and do not connect them with an underscore, you will get an error. Take a look at the following screenshot and see where it says SyntaxError: invalid syntax. Notice this error occurred because the height centimeters variable did not have an underscore to connect the words:

Naming variables – conventions to follow

What can variables remember?

Python variables can be programmed to remember all kinds of information! You will notice in our original example that we stored a word and then a number. There are three different kinds of information that we will be using to build our calculator in Chapter 3, Calculate This!, strings, integers, and floats. Each bit of information is input and output a little differently.

Strings

In Python, a string is any piece of data that's captured between two single quote marks, that is, these symbols ' '. Sometimes, double quotation marks are used. For example, I can have a string variable that looks like this:

sentence = 'This is a sentence about Python.'

This string variable contains letters and words. Most string variables do. However, you can store a number as a string also as long as that number is in single quotes:

  number_string = '40'

If we can store all kinds of information as strings, why do we need other data types? Well, when we store numbers as strings, we cannot do math with the numbers! Type this problem into your Python shell, and then you will see why we need data types besides strings:

  first_number = '10'
  second_number = '20'
  print(first_number + second_number)

What happened in your Python shell? You might have expected the printed output to be 30 since 10 plus 20 is equal to 30. However, Python saw each number as a text string and simply put the two text strings next to each other. So, your result was probably 1020. Here is how this looks in the Python shell:

Strings

Integers

Computers are really great at math, and math will allow us to execute more complicated programs, such as games. Python stores whole number data as integers.

Let's start with integers:

  • An integer is simply a plain whole number. If we want to make our variables store integers, we would take away the quotes.
  • Then, when we add the two variables and print the output, we will get a mathematical result.

Try it out! Let's do some math with these variables:

  1. Type the following two variables in your Python shell:
        first_number = 10
        second_number = 20
    
  2. Then, print the output by typing print and the variables:
        print(first_number + second_number)
    

After completing step 2, press Enter. Your result should be 30. This is because Python is reading the numbers as integers, and Python understands integers with mathematical operators. In fact, Python understands math so well that you will notice that no equals sign was needed to tell Python to output the answer. Take a look at this screenshot:

Integers

Floating point numbers (floats)

Hopefully, you now better understand how Python works with integers (whole numbers). However, people and computers often need to work in fractional numbers. In Python, these numbers are called floating point numbers, but many people call them floats as a shortcut:

  • Floats are actually a really fancy way of saying numbers using decimals
  • Floats are called this because the decimal point can be anywhere among the numbers, allowing for decimals of many different sizes
  • Setting numbers as floats allows us to do more complicated math using fractional numbers
  • To set a variable to a float, you don't have to do anything special or different from what you did to set the integers
  • Python knows that a number input (a variable, for example) with a decimal point is a float, and Python will output the answers as a float if the problem is clear

In your Python shell, try this math problem using floating point numbers instead of integers:

  first_number = 10.3
  second_number = 20.3
  print(first_number + second_number)

This time, in your Python shell, you should notice that Python recognized the variable input as floating point numbers and was able to output the complete and correct answer without us having to use additional instructions. The output from your print statement should be 30.6, as you can see in this screenshot of the Python shell:

Floating point numbers (floats)

Combining strings, integers, and floats

So far, we have only attempted to combine items that share a data type. We have added two strings, two integers, or two floats. What happens when you try to add two different types of information, such as a string and an integer? In your Python shell, type the following lines of code and pay attention to the output:

  first_number = '10'
  second_number = 20
  print(first_number + second_number)

You are likely to notice the error that you receive. The important line to pay attention to is TypeError: cannot concatenate 'str' and 'int' objects. Python is telling us that it cannot work with these two different data types and that makes sense. So, if you do make a mistake in your typing or try to execute an operation in two different data types, you may get an error like this:

Combining strings, integers, and floats
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.210.17