Section Summary
Key Takeaways
Literals are notations for representing some fixed value in code. Python has various types of literals:
a literal can be a number (numeric numbers e.g.
123
)or a string (string literals e.g. "I am a literal")
The binary system is a system that uses 2 as the base and is made up of 0's and 1's
The octal system is a system that uses 8 as the base
The hexadecimal system uses 16 as the base and uses the decimal numbers and A-F letters
Integers are whole positive or negative numbers
Floats or Floating Point are numbers that contain a fracrional componenet (e.g.
1.27
)To encode an apostrophe or quote inside a string you can either use the escape character eg
'I\'m happy.'
or open and close the string using an opposite set of symbols (double quote) to the ones you which to encode eg"I'm happy"
to encode an apostrophe, and a single quote to encode a double quote eg.'He said "Python", not "Typhoon" '
Boolean values are either True or False and represented by
1
and0
respectivelyThere is another type of literal: the
None
literal and is used to represent the absence of a value
Exercise
Exercise 1
What types of literals are the following two examples? "Hello ", "007"
They are both strings/string literals
Exercise 2
What types of literals are the following four examples? "1.5", 2.0, 528, False
They are string, floating-point, integer and Boolean literals
Exercise 3
What is the decimal value of the following binary number? 1011
- The answer id 11
8 4 2 1
1 0 1 1 ------- Add up 8+2+1 = 11
Last updated
Was this helpful?