Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

LAB

Estimated time

5-10 minutes

Level of difficulty

Easy

Objectives

  • becoming familiar with the print() function and its formatting capabilities
  • practicing coding strings
  • experimenting with Python code

Scenario

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. 

swiftCopy code

print("I'm\n\"learning\"\n\"\"\"Python\"\"\"")

Output:

rustCopy code

I'm "learning" """Python"""

Explanation:

  • The newline character \n is used to break the output into three lines.
  • Double quotes " are used to enclose the strings "learning" and "Python". To include double quotes within a string, we need to escape them with a backslash \ to avoid interpreting them as the end of the string.
  • The single quote in "I'm" is not escaped because we used double quotes to enclose the entire string.

Key takeaways

  1. 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.")
  2. The binary system is a system of numbers that employs as the ase. Therefore, a binary number is made up of 0s and 1s only, e.g., 1010 is the 10 in decimal. 
    Octal and hexadecimal system numeration systems, similarly, employ and 16  as their bases respectively. The hexadecimal system uses the decimal numbers and six extra letters. 
  3. Integers (Or simply ints) are one of the numerical types supported by Python. They are numbers written without a fractional component, e.t., 256 , or -1 (negative integers).
  4. 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.t., 1.27. 
  5. To encode an apostrophe or a quote inside a string you can either use the escape character, e.t., '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 a apostrophe, and 'He said "Python", not "typhoon"' to encode a double quote. 
  6. Boolean values are two constant objects True and False used to represent truth values (in numeric contexts 1 is True, while 0 is False. 

EXTRA

There is one more, special literal that is used in Python: the None literal. This literal is a so called NoneType object, and it is used to represent the absence of a value. We'll tell you more about it soon. 

Exercise 1

What types of literals are the following two examples? 

"Hello", "007"

They're both strings/string literals. 

Exercise 2

What types of literals are the following four examples? 

"1.5", 2.0, 528, False

String, numerical literal (a float), the third is a numerical literal (an integer) and the fourth is a boolean literal. 

Exercise 3

What is the decimal value of the following binary number? 

1011

It's 11, because (2**0) +(2**1) + (2**3)=11