14 Basic Features for Learning Python + Final Exercise. Part II

Daniel Morales
Mar 30, 2020

Following the first part of 14 Basic Features for Learning Python, in this second part we are going to see the last 7 Basic Python Features:

  • Changing Numbers
  • Exponential
  • The Module
  • Concatenation
  • The Plus-Equals
  • Multiline Strings
  • Exercises

Feature #8 - Changing Numbers

Numerical values assigned to variables can be treated in the same way as if they were numbers themselves.

Two variables can be added or divided, for example.  

ice_cream_price = 1.50
number_of_ice_creams = 4

# prints "6.0"
print(ice_cream_price * number_of_ice_creams)

# prints "1.5"

#prints #4

# Updating the price
ice_cream_price = 2

# prints "8.0"
print(ice_cream_price * number_of_ice_creams)

# prints "2"

#prints #4

Feature #9 - Exponential

Python can run exponentially. In math you should write it small and in the upper right

Since it is a multiplication operation, we use the ** notation to make exponentiation in python

We can create a number to the 10, square, cube or square root as follows

# 2 to the 10th power, or 1.24
print(2 ** 10)

# 8 squared, or 64
print(8 ** 2)

# 9 * 9 * 9, 9 cubed, or 729
print(9 ** 3)

# We can even perform fractional exponents, 4 to the half power, or 2
print(4 ** 0.5)

Feature #10 - The Module

It is indicated by the % symbol and yields the remainder of a division. If the number is divisible, then the result of the module will be 0. It is useful when we need to run actions every N number of times.

# prints 4 because 29/5 is 5 with a remainder of 4
print(29 % 5)

# prints 2 because 32/3 is 10 with a remainder of 2
print(32 % 3)

# Modulo by 2 returns 0 for even numbers and 1 for odd numbers
# print 0
print(44 % 2)

Feature #11 - Concatenation

The + operator not only adds two numbers, but can also "add" two strings. This is called concatenation

If we want to concatenate a string with a number we must change the number to a string with the python str() function.

my_birthday = "I am "
age = 19
my_birthday_two = " years old"

# Concatenating an integer with strings is possible if we turn the integer into a string first
full_string = my_birthday + str(age) + my_birthday_two

# prints "I am 19 years old"

Feature #12 - The Plus-Equals

This is an abbreviated form of updating variables. When you have a number stored in a variable and you want to add another value to the current value of the variable, you can use the += operator

Can also be used with string concatenation

# we have a variable with a number saved
number_km_runned = 10

# we need to update with another 2 kms today
number_km_runned += 2

# when we print, the new value is the old value plus the number after plus-equals
#Prints 12

Feature #13 - Multiline Strings

If you need to write multiple lines in a string you can use triple quotes: """ or ''' instead of both open and close

lorem_ipsum = """
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia 
deserunt mollit anim id est laborum.


Final Exercises

Try to follow these exercises without looking at the solution below. This will help you improve your skills, but at the same time if you have any doubts, you can obviously take a look at the solutions.

1- Print your name using the print() command.

2- If your print used double quotes " change them to single quotes. If you used quotes ' change them to double quotes.

3- Type the variable meal and assign it the value of breakfast and print it. Then change the value assigned by launch and print it.

4- Where is the error in the next line of code? meal = "launch, breakfast and "tea". Save in a string your answer and assign it to a variable called error_found. Now in a new variable called error_type assign it a string with the error type: "sintax error" or "name error".

5- Write in different variables the last 3 movies you saw and assign them their own rating (it can be floating) according to your criteria.

6- Print the result of the following equation: 25 * 68 + 13 / 28

7- Print the result of the following operation: 80 / 0

8- You've decided to get into the world of quilt weaving! To calculate the number of squares you will need for your first quilt, let's create two variables: quilt_width and quilt_length. Let's make this first quilt 8 squares wide and 12 squares long. Print out the number of squares you will need to create the quilt!

9- Concatenate the following strings and save the message they form in the message variable.

string1 = "The wind, "
string2 = "which had hitherto carried us along with amazing rapidity, "
string3 = "sank at sunset to a light breeze; "
string4 = "the soft air just ruffled the water and "
string5 = "caused a pleasant motion among the trees as we approached the shore, "
string6 = "from which it wafted the most delightful scent of flowers and hay."
10- We are shopping online and found a new pair of sneakers. Just before we leave, we find a nice sweater and some books that we want to buy too! Use the += operator to update the total_price to include the prices of nice_sweater and fun_books.

The product and price you already have in the shopping cart is: new_shoes = 50.00. The prices of the new items added to the cart are: nice_sweater = 39.00 and fun_books = 20.00.

In this second part of the series of articles, we saw other 7 functionalities that allow us to work with Python and finally some exercises that will allow you to consolidate this knowledge. 

Exercise Solution

quilt_width = 8
quilt_length = 12
print(quilt_width * quilt_length)

quilt_length = 8
print(quilt_width * quilt_length)


total_price = 0

new_sneakers = 50.00

total_price += new_sneakers

nice_sweater = 39.00
fun_books = 20.00
# Update total_price here:

total_price += nice_sweater
total_price += fun_books

print("The total price is", total_price)

“14 Basic Features for Learning Python + Final Exercise. Part II”
– Daniel Morales twitter social icon Tweet

Share this article:


Post a comment
Log In to Comment

Related Stories

Oct 16, 2021

6 Advanced Statistical Concepts in Data Science

The article contains some of the most commonly used advanced statistical concepts along with their Python implementation.In my previous articles Be...

Nagesh Singh Chauhan
By Nagesh Singh Chauhan
Oct 09, 2021

Top 10 Python Extensions for Visual Studio Code

In this new post we want to talk about the most useful Python extensions for Visual Studio Code. Visual Studio Code is an integrated development en...

Daniel Morales
By Daniel Morales
Sep 25, 2021

10 Highly Probable Data Scientist Interview Questions

The popularity of data science attracts a lot of people from a wide range of professions to make a career change with the goal of becoming a data s...

Soner Yıldırım
By Soner Yıldırım

Join our private community in Slack

Keep up to date by participating in our global community of data scientists and AI enthusiasts. We discuss the latest developments in data science competitions, new techniques for solving complex challenges, AI and machine learning models, and much more!

We'll send you an invitational link to your email immediatly.
arrow-up icon