Python Lesson 2: Variables & Number Types in Python (50th Post)

Advertisements

Hello everybody,

It’s Michael, and today’s lesson will be on rudimentary coding with Python. Since most of the previous post focused on the background and setup process of Python, this lesson will focus on some basic coding with Python.

First, let’s open up Anaconda and launch our Spyder* console:

Screen Shot 2019-08-06 at 2.12.42 PM

*By the way, Spyder stands for Scientific PYthon Development Environment. I don’t know why I forgot to mention this in my last post.

Now, let’s start with a simple Python program. Here’s the code:

x = 14
y = x * 2
z = “14-Doubled”
print(x, y, z)

And here’s the output:

14 28 14-Doubled

In this simple program, I demonstrated the concept of variables in Python. They work quite similarly to variables in Java, but with one notable difference-there is no need to declare variable type in Python. So writing something like int x = 14 would be unnecessary in Python (and I think doing so will generate an error).

Keep in mind that, even though syntax might be different, variable naming rules for Python are just like those for Java. First of all, variable names must start with either a letter or underscore; they can NEVER start with a number. Also, variables names can only contain letters, numbers, and underscores, so don’t include special characters like &, *, or %. Finally, remember that, like in Java, variable names in Python are also case sensitive, so walk, Walk, and WALK would be three different variables.

Also, take a look at the print function. Notice that I used commas to combine the variables, not plus signs (which is what I would use to print several variables on one line in Java). However, you would use a plus sign to combine text and a variable, like this (I used z from the example I just discussed):

print(“Twenty eight is ” + z)

Twenty eight is 14-Doubled

One cool thing about Python variables is that you can assign values to several variables on the same line. Here’s how (along with the program output):

a, b, c, d = “ant”, “bee”, “cat”, “dog”
print(a)
print(b)
print(c)
print(d)

And here’s the output:

ant
bee
cat
dog

First, you would simply write each of the variable names, separated by a comma. After you insert an equal sign, write the values of the variables separated by a comma. You only need the pair of quotation marks if the values are Strings, otherwise they aren’t necessary.

  • Keep in mind that the variable names and their corresponding values are listed in order. In other words, the first variable a corresponds to the first value ant, the second variable b corresponds to bee, and so on.

Another interesting thing you can do with Python variables is assigning the same value to several variables on the same line. Here’s how (I included a program along with sample output):

a = b = c = d = “The quick brown fox jumps over the lazy dog”
print(a)
print(b)
print(c)
print(d)

And here’s the output

The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

The idea is similar to that of the previous program (assigning values to variables in the same line). However, there is one major difference (aside from assigning a single value to multiple variables)-each variable name is separated with equals signs, not commas. Equals signs are used because you’re assigning a single value to four variables, thus, each variable would have the same value as the others.

  • By the way, anybody recognize the sentence I used in this example?

One more topic I wanted to discuss was number types in Python, which are different from number types in Java. First of all, there are only three number types in Python (int,float, and complex) as opposed to the six number types in Java (int, byte, long, short, double, and float).

The fact that there are only three number types, as opposed to six, makes Python simpler in this regard. After all, even though I like using Java, I find that the several different number types are redundant. After all, byte, long, and short are all integers with the only difference between each number type is their range of possible values. Likewise, float and double are both decimals, but the only major difference is the same as with the integers types-their range of possible values is different. In Python, int is the only possible number type for integers and float is the only possible number type for decimals.

  • Keep in mind that float can be either a simple decimal (e.g. -1.04, 21.335) or a number in scientific notation with an e to indicate a power of 10 (e.g. 5.3e6, 4.55e-1).

Now, you probably have never seen the complex number type before. After all, Java doesn’t have this number type.

Complex represents complex numbers, which are the combination of real and imaginary numbers. Some examples of complex numbers include 3+7i, 4-2i and 15+10i. If you see a pattern in all of these numbers, it’s because they are all written in the form a+bi, where a is the real number and bi is the imaginary number. However, numbers such as -3j, 5j, and 32j are also complex numbers since they have both a real and imaginary part.

  • If you’ve taken Algebra 1, you might remember the concept of imaginary numbers. But if you don’t recognize or remember the concept of imaginary numbers, click this link to learn more-Imaginary Numbers.
  • The i is replaced with a j in Python. You must use a j when writing any complex numbers because you will get an error if you try to use another letter, including i.

One cool thing about Python is that you can easily convert from one number type to another. However, the one exception to this rule is that you can’t convert complex numbers into float or int.

Here’s an example of Python number conversion (the sample program along with the output):

x = 2019
y = 8.07
z = 20j

print(x)
print(y)
print(z)

a = float(x)
b = int(y)
c = complex(x)

print(a)
print(b)
print(c)

Here’s the output:

2019
8.07
20j
2019.0
8
(2019+0j)

The top three lines include variables x, y, and z, which are of type int, float, and long, respectively; these variables are printed on the first three lines of the output.

Pay attention to the seventh through ninth lines of code, since they contain the number type conversions (these are variables a, b, and c). A will convert the integer x into a float. B will convert the decimal y into an int. C will convert the integer x into a complex number. The converted values are then displayed in the second three lines of the output.

  • If you noticed, I didn’t use z in any of the conversions. This is because z is a complex number, and remember that complex numbers can’t be converted into anything else.

Thanks for reading,

Michael

Leave a ReplyCancel reply