Module 2.2 - Python Literals

Ace your homework & exams now with Quizwiz!

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

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.

Python, then, is a binary reptile. These two Boolean values have strict denotations in Python:

True False You cannot change anything - you have to take these symbols as they are, including case-sensitivity. Challenge: What will be the output of the following snippet of code?

Boolean Algebra

a part of algebra which makes use of only two distinct values: True and False, denoted as 1 and 0.

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.").

Literal

data whose values are determined by the literal itself

You use literals to

encode data and to put them into your code

by adding a minus. You can write: -11111111, or -11_111_111. Positive numbers do not need to be preceded by the plus sign, but it's permissible, if you wish to do it. The following lines describe the same number: +11111111 and 11111111

how do we code negative numbers in Python

Binary System

is a system of numbers that employs 2 as the base. Therefore, a binary number is made up of 0s and 1s only, e.g., 1010 is 10 in decimal.

don't forget this simple rule - you can omit zero when In essence, you can write the value 0.4 as: .4 For example: the value of 4.0 could be written as: 4. This will change neither its type nor its value.

it is the only digit in front of or after the decimal point.

A Boolean Value

Each time you ask Python if one number is greater than another, the question results in the creation of some specific data

Octal Value This means that the number must contain digits taken from the [0..7] range only

If an integer number is preceded by an 0O or 0o prefix (zero-o), it will be treated as an

Hexidecimal Number 0x123 is a hexadecimal number with a (decimal) value equal to 291

If an integer number is preceded by the prefix 0x or 0X (zero-x), it will be treated as a

A physical constant called Planck's constant (and denoted as h), according to the textbooks, has the value of: 6.62607 x 10-34.

If you would like to use it in a program, you should write it this way: 6.62607E-34 Note: the fact that you've chosen one of the possible forms of coding float values doesn't mean that Python will present it the same way. Python may sometimes choose different notation than you. For example, let's say you've decided to use the following float literal: 0.0000000000000000000001 When you run this literal through Python: print(0.0000000000000000000001) this is the result: 1e-22 output Python always chooses the more economical form of the number's presentation, and you should take this into consideration when creating literals.

The first is based on the concept we already know of the escape character, which you should remember is played by the backslash. The backslash can escape quotes too. A quote preceded by a backslash changes its meaning - it's not a delimiter, but just a quote. This will work as intended: print("I like \"Monty Python\"") Note: there are two escaped quotes inside the string - can you see them both? The second solution may be a bit surprising. Python can use an apostrophe instead of a quote. Either of these characters may delimit strings, but you must be consistent. If you open a string with a quote, you have to close it with a quote. If you start a string with an apostrophe, you have to end it with an apostrophe. This example will work too: print('I like "Monty Python"')

Let's assume that we want to print a very simple message saying: I like "Monty Python" How do we do it without generating an error? There are two possible solutions.

Underscores you can write this number either like this: 11111111, or like that: 11_111_111 Positive numbers do not need to be preceded by the plus sign, but it's permissible, if you wish to do it. The following lines describe the same number: +11111111 and 11111111

Python allows the use of ......... in numeric literals

Type

The characteristic of the numeric value which determines its kind, range, and application

Boolean

The name comes from George Boole (1815-1864), the author of the fundamental work, The Laws of Thought, which contains the definition of Boolean algebra - a part of algebra which makes use of only two distinct values: True and False, denoted as 1 and 0.

e When you want to use any numbers that are very large or very small, you can use scientific notation Take, for example, the speed of light, expressed in meters per second. Written directly it would look like this: 300000000. To avoid writing out so many zeros, physics textbooks use an abbreviated form, which you have probably already seen: 3 x 10^8. It reads: three times ten to the power of eight. In Python, the same effect is achieved in a slightly different way - take a look: 3E8 The letter E (you can also use the lower-case letter e - it comes from the word exponent) is a concise record of the phrase times ten to the power of. Note: the exponent (the value after the E) has to be an integer; the base (the value in front of the E) may be an integer.

it's not only points that make a float. You can also use the letter

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. "I'm" ""learning"" """Python"""

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

How do you embed an apostrophe into a string placed between apostrophes? Try to print out a string containing the following message: I'm Monty Python.

print('I\'m Monty Python.') or print("I'm Monty Python.")

Floating-point Numbers (or simply floats)

that contain (or are able to contain) the fractional part

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.

Integers

those which are devoid of the fractional part

boolean values are used to represent a very abstract value

truthfulness


Related study sets

Chapter 11: Groups and Interests

View Set

Spanish 2, Capitulo 2, Desafio 1

View Set

Principles of Marketing Exam Chapter Seven

View Set

Chapter 14: Accounts Payable and Other Liabilities

View Set