Categories:

Variables and Types for Python

Python, as an object-oriented language, is quite straightforward and fun to use. In Python, you do not have to declare a variable prior to their usage (or select their type); all variables in Python are objects.

We will go over some basic types of variables. For the following examples, feel free to alter them to however way you’d wish to do so.

Numbers

In Python, they support two types of numbers. They support both integers and floating points. The difference is that integers are whole numbers while floating-point numbers are decimals. There is a third type that is supported, and that is a complex number. A complex number is an extension of the familiar real number system, in which all numbers are expressed as a sum of a real part and an imaginary part.

To define an integer, you can utilize the following syntax:

x = 21
print(x) # 21

For a floating point, you can define it with the following:

x = 1.0
print(x) # 1.0

You can also use this format for a float:

x = float(7)
print(x)

Strings

Strings are different from numbers, they are just a sequence of characters. To note, strings are immutable; this would imply that once you define the string, you cannot alter it.

In python, you can define a string with a single quote or double.

x = 'pen' # single quote
print(x)
y = "pineapple" # double quote
print(y)

So what is the difference? The difference between the two is that it is a lot easier to incorporate apostrophes with double quotes than it is with a single quote (single quotes could terminate the string, within the presence of apostrophes and such).

x = "I'm walking here! I'm walking here!" 
print(x)

With simple operators, you can achieve various tasks with strings and numbers.

num1 = 1
num2 = 2
sum_of_nums = num1 + num2
print(sum_of_nums) # 3

string1 = "I got a Pen."
string2 = "I got an Apple."
lyrics = string1 + " " + string2
print(lyrics) # I got a Pen. I got an apple.

This is another interesting way of doing an assignment, we can simultaneously do assignments on the same line:

x, y = 1, 2
print(x,y) # 1 2

In Python, you cannot mix operators (between numbers and strings). If you do so, you would get a TypeError that would state: TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’.

x = 6
y = 9
z = "hakuna matata"

print(x + y + z) # This will fail

# You would get the following errors:
# Traceback (most recent call last):
# File "<stdin>", line 5, in <module>
# print(x + y + z)
# TypeError: unsupported operand type(s) for +: 'int' and 'str'

Exercise

Let’s try out an exercise. For this exercise, we will try and practice strings, integers, and floating-point numbers. Let’s try tweaking the following code so that “x” equals “How’s life!,” “y” equals “1.0” and “z” equals “1.” Try out the following script and see if I can achieve the desired results.

Script:

# alter this code
x = None
y = None
z = None

# testing code
if x == "How's life!":
    print("String: %s" % x)
if isinstance(y, float) and y == 1.0: 
    print("Float: %f" % y)
if isinstance(z, int) and z== 1:
    print("Integer: %d" % z)

Solution:

# alter this code
x = "How's life!"
y = 1.0
z = 1

# testing code
if x == "How's life!":
    print("String: %s" % x)
if isinstance(y, float) and y == 1.0:
    print("Float: %f" % y)
if isinstance(z, int) and z== 1:
    print("Integer: %d" % z)

Output:

<script.py> output:
    String: How's life!
    Float: 1.000000
    Integer: 1

For more information

If you seek to learn more about Python, please visit Python’s official documentation. In a future guide or update, we will go over the utilization of things like carriage returns, Unicode characters backslashes, and so on.