EUSTAQUIO, John Patrick A. Prof. Lituanas Elemento MEE21: Experiment 2: Python Literals and Operators
EUSTAQUIO, John Patrick A. Prof. Lituanas Elemento MEE21: Experiment 2: Python Literals and Operators
• To become familiar with the print() function and its formatting capabilities;
• To practice coding strings using literals; and
• To experiment with Python code using various operators.
Background:
Literals:
Literals are notations for representing some fixed values in code. Python has various types of
literals - for example, a literal can be a number (numeric literals, e.g., 123), or a string (string literals,
e.g., "I am a literal.").
The number be in varoius forms, such as, decimal, binary, octal, and hexadecimal. The binary
number uses the base 2. It can be made up of 0s and 1s only, e.g. 1010 or equivalent to 10 in decimal.
Octal and hexadecimal numeration systems, similarly, employ 8 and 16 as their bases
respectively. The hexadecimal system uses the decimal numbers and six extra letters.
Integers (or simply ints) are one of the numerical types supported by Python. They are numbers
written without a fractional component, e.g., 256, or -1 (negative integers).
Floating-point numbers (or simply floats) are another one of the numerical types supported by
Python. They are numbers that contain (or are able to contain) a fractional component, e.g., 1.27.
To encode an apostrophe or a quote inside a string you can either use the escape character,
e.g., 'I\'m happy.', or open and close the string using an opposite set of symbols to the ones you wish to
encode, e.g., "I'm happy." to encode an apostrophe, and 'He said "Python", not "typhoon"' to encode a
(double) quote.
Boolean values are the two constant objects True and False used to represent truth values (in
numeric contexts 1 is True, while 0 is False.
Operators:
Operators are special symbols or keywords which are able to operate on the values and perform
(mathematical) operations, e.g., the * operator multiplies two values: x * y.
1
Arithmetic operators in Python: + (addition), - (subtraction), * (multiplication), / (classic
division - returns a float if one of the values is of float type), % (modulus - divides left operand by right
operand and returns the remainder of the operation, e.g., 5 % 2 = 1), ** (exponentiation - left operand
raised to the power of right operand, e.g., 2 ** 3 = 2 * 2 * 2 = 8), // (floor/integer division - returns a
number resulting from division, but rounded down to the nearest whole number, e.g., 3 // 2.0 = 1.0)
Materials:
- Computer with Internet and Python installed.
Write a one-line piece of code, using the print() function, as well as the newline and escape
characters, to match the expected result outputted on three lines.
Expected output
"I'm"""learning"""""Python"""
print ("I'm\n")
print ("learning\n")
print ("Python\n")
2
2. Observe the output for each Exponentiation Arithmetic Operation.
print(2 ** 3)
print(2 ** 3.)
print(2. ** 3)
print(2. ** 3.)
Based from the observation the symbol “**” means “raised to”. For example. (3 ** 4)means that,
print 3 is raised to 4. In the first code, print (2 ** 3) , the answer coded in python 3.8 is 8. From the
second code to the last code which are print (2 ** 3.), print (2. ** 3), and print (2. ** 3.), the answer in
python is 8.0. the difference between all of them is that, there are points in the codes. For example, is
print (2 ** 3.) and on the first part of the code . Which is print (2 ** 3).There are no points as commanded.
3
print(2 * 3)
print(2 * 3.)
print(2. * 3)
print(2. * 3.)
Based from the observation, the symbol “*” means multiplication in python. So, print(2 * 3)
means (2x3) which is 6. In the first code, print (2 *3) doesn’t have decimal form and the answer is 8.
Then from the second to the last code, there is a decimal on the middle which is 6.0. Their difference is
just the same as number 2 on which there are no decimals on codes without decimals, and there are
decimals on codes with decimals.
4
4. Observe the output for each Division Arithmetic Operation.
print(6 / 3)
print(6 / 3.)
print(6. / 3)
print(6. / 3.)
Based from the observation, the symbol “/” means division. In the code “print (6 / 3)” means “6
divided by 3”. In addition to that, the code with or without the decimals, the answer on the code will
always have a decimal. So, if I input print(6 / 3), the answer is 2.0, and if I input print(6 / 3.), the answer
is also 2.0.
5
5. Observe the output for each Integer Division Arithmetic Operation.
print(6 // 3)
print(6 // 3.)
print(6. // 3)
print(6. // 3.)
print(6 // 4)
print(6. // 4)
Based from the observations, the symbol “//” means division with rounded down to the nearest
decimal form. If i input print (6 // 3), Then the answer will be 2. If I input print (6 //3.) then the answer
will be 2.0. This is due to reason that, it has decimal on the code. If I input print (6 // 4) then the answer
should be 1.5 , and since we used the symbol “//”, it will rounddown to the nearest decimal so it becomes
1. Lastly, if I input (6 // 4.), the answer would also be 1.0 since it has decimal on the code.
print(-6 // 4)
print(6. // -4)
Yes, The results can be predicted, obviously it will become a division arithmetic operation. Then
the answer is not rounded down because it did not use the symbol “//”. So instead of negative 1 (-1), the
answer is negative 1.5 (-1.5).
6
7. Observe the output for each Remainder Arithmetic Operation.
print(14 % 4)
print(12 % 4.5)
Based from the observation, the symbol “%” means remainder. So, if I input print (14 % 4) in
python, it commands the computer to divide 14 by 4, the answer would be 3 with a remainder of 2. So
the answer would be 2. If I input print (12 % 4.5) the answer would be 2 with a remainder of 2.67, but it
is rounded up to the nearest decimal, so the answer would be 3.
8. Observe the output for each Addition Arithmetic Operation.
print(-4 + 4)
print(-4. + 8)
print ( -1. + 1)
Based from the observations, the symbol “+” means addition. Firstly, if I input print (-4 +4), it
means -4+4 which is 0. Secondly, If I input print (-4. + 8), it would result to 4.0 becasue it has decimal.
And then lastly, if I input print (-1 + 1) then the answer is 0.0. There will always be a decimal place if I
input decimal on the code.
7
Exercises:
1. What is the output of the following snippet?
print((2 ** 4), (2 * 4.), (2 * 4))
8
Conclusion:
Using Python 3.8, the Student Engineers where able to attain the knowledge and be familiar with the
Print() function and its formatting abilities. They also learned that putiing decimal on the code make a
difference to the output. Also they understood that using the proper symbols is a must to avoid errors.
Recommendation:
As a student engineers of De Lasalle Dasmarinas University. Learning python give us convenience.
Because python is an endless coding platform that the students can apply when they want to begin simple
coding. Which could develop new ideas which could also help the convenience of others. We recommend
future student engineers to explore the python programming furthermore to better understand its’
concepts and capabilities.
References:
https://708816701.netacad.com/courses/1054472/modules/items/68994012