Bytes

Literals in Python

Module - 2 Basics of Python Programming
Literals in Python

In Python, a literal 🔢 is a notation representing a fixed value in the code. It is a constant value that doesn't change 🚫 during the execution 💻 of the program. 📚 This lesson will provide 📝 a clear understanding 🧠 of Python literals.

What are Literals in Python?

Python Literals

Python Literals

There was a software engineer named Alie who needed to learn Python. She chose to begin with the basics; one of the primary things she learned about was literal. 💡 Alie knew that in Python, literals are fixed values that represent constant data in Python code. They are utilized to assign values to variables, define constants, and perform different operations within the code. 💻 She was presented to a few types of literals, such as numeric literals, string literals in python, boolean literals, None literal, and container literals 📦. To better understand these literals in python, Alie decided to utilize them in a story. 📖 This lesson will examine diverse types of literals in python and their utilization.

Types of Literals in Python

Types of Literals

Types of Literals

Alie knew Python supports several types of literals in python, including:

  1. Numeric Literals
  2. String Literals
  3. Boolean Literals
  4. None Literal
  5. Container Literals

Numeric Literals in Python

In Python, a numeric literal is a notation representing a fixed numeric value in the code. Python has three types of numeric literals: integer literals, floating-point literals, and complex literals.

  1. Integer literals: Integer literals are used to represent whole numbers in Python. They can be positive, negative, or zero. Examples include:
42 # positive integer 
-123 # negative integer 
0 # zero
  1. Floating-point literals: Floating-point literals are used to represent decimal numbers in Python. They can be positive, negative, or zero and written using scientific notation. Examples include:
3.14 # positive floating-point number
- 2.5 # negative floating-point number
4.2e-3 # positive floating-point number written in scientific notation
  1. Complex literals: Complex literals are used to represent complex numbers in Python. They consist of a real part and an imaginary part, which are both floating-point numbers. The imaginary part is denoted by the letter "j." Examples include:
- 2 + 3j # complex number with a real part of 2 and an imaginary part of 3
- 1.5 - 2.5j # complex number with a real part of -1.5 and an imaginary part of -2.5

Numeric literals in python are used in mathematical operations, comparisons, and other numeric computations. She started using numeric literal to represent the ages of different characters in her story. For example, she wrote:

alie_age = 25
bobby_age = 30
john_age = 35

String Literals in Python

String literals in python represent strings of characters in Python. They are enclosed in single quotes, double quotes, or triple quotes. Examples include:

- 'hello' # a string literal enclosed in single quotes
- "world" # a string literal enclosed in double quotes
- '''Python is a programming language. It is used for web development, scientific computing,artificial intelligence, and more.''' # a string literal enclosed in triple quotes

Next, Alie used string literals to represent the characters' names in her story. She wrote:

alie_name = 'Alie'
bobby_name = "Bobby"
john_name = '''John'''

Boolean Literals in Python

Boolean literals represent the truth values True and False in Python. Examples include:

- True # a boolean literal representing true
- False # a boolean literal representing false

Alie also used boolean literals to represent whether the characters were friends. She wrote:

alie_and_bob_are_friends = True
bob_and_charlie_are_friends = False

None Literal in Python

The None literal represents a null value in Python. Examples include:

None # a None literal representing null

Container Literals In Python

Container literals represent data structures that can contain other literals in python.

  1. List Literals: List literals are used to represent a list of values. They are enclosed in square brackets and separated by commas.
  2. Tuple Literals: Tuple literals are used to represent a sequence of values. They are enclosed in parentheses and separated by commas.
  3. Dictionary Literals: Dictionary literals are used to represent key-value pairs. They are enclosed in curly braces and separated by commas.
- [1, 2, 3] # a list literal containing integers
- {'name': 'Alie', 'age': 25} # a dictionary literal containing key-value pairs
- (1, 2, 3) # a tuple literal containing integers

Finally, Alie used container literals to different group types of literals. For example, she used a literal list to represent the different items in Alie's backpack:

alie_backpack = ['book', 'water bottle', 'snacks']
alie_info = {'name': 'Alie', 'age': 25, 'is_friend': True}
bobby_info = {'name': 'Bobby', 'age': 30, 'is_friend': True}
john_info = {'name': 'John', 'age': 35, 'is_friend': False}

These literals in python are used to represent constant values in Python programs, and they cannot be modified once they are defined.

Conclusion

In conclusion, Python literals 🔢🔤📃 represent constant values in Python code. They cannot be modified during the execution of the program. Understanding literals in python is crucial for writing efficient and readable code. Python supports different types of literals, such as numeric literals 🔢, string literals 🔡, boolean literals, None literal, and container literals 📦.

Key Takeaways on Literals in Python

  • Literals in python are fixed values that represent constant data in Python code. They are utilized to relegate values to variables, define constants, and perform different operations within the code. 💻
  • Python has a few types of literals, including numeric literals, string literals, boolean literals, None literal, and container literals. 📦
  • Numeric literals incorporate integer, floating-point, and complex literals, which are utilized in numerical operations, comparisons, and other numeric computations. 🔢
  • String literals in python represent strings of characters in Python and are encased in single quotes, double quotes, or triple quotes. 🔤
  • Boolean literals represent the truth values Genuine and Wrong in Python. ✔️❌
  • The None literal represents a null value in Python. 🅾️
  • Container literals represent data structures that can contain other literals, counting list literals, tuple literals, and dictionary literals. 📦
  • Literals in python can be utilized to gather diverse literals and represent complex data structures.🤝

Quiz

  1. What do you mean by python literals? 
    1. Fixed values that represent constant data in Python code. 
    2. Variable values that can be changed during the execution of the program.  
    3. Values that are used to perform mathematical operations in Python. 
    4. Values that are used to perform logical operations in Python.

Answer: a) Fixed values that represent constant data in Python code.

  1. How many types of numeric literals are there in Python? 
    1. 5

Answer: b) 3

  1. How are string literals enclosed in Python?
    1. In square brackets
    2. In parentheses
    3. In single quotes, double quotes, or triple quotes
    4. In curly braces

Answer: c) In single quotes, double quotes, or triple quotes

  1. What does the None literal represent in Python?
    1. A null value
    2. An empty value
    3. A false value
    4. A true value

Answer: a) A null value

  1. Which type of literal is used to represent a sequence of values in Python?
    1. List literals
    2. Tuple literals
    3. Dictionary literals
    4. Container literals

Answer: b) Tuple literals

  1. Which type of literal is used to represent a data structure that can contain other literals in Python? 
    1. List literals 
    2. Tuple literals 
    3. Dictionary literals 
    4. Container literals

Answer: d) Container literals

Online Python Compiler (Editor / Interpreter)
Open Compiler
Recommended Courses
Certification in Full Stack Data Science and AI
Course
20,000 people are doing this course
Become a job-ready Data Science professional in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.
Masters Program in Data Science and Artificial Intelligence
Course
20,000 people are doing this course
Join India's best Masters program in Data Science and Artificial Intelligence. Get the best jobs in top tech companies. Accredited by ECTS and globally recognised in EU, US, Canada and 60+ countries.

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter.

avatar
Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter
Explore Courses

Vikash SrivastavaCo-founder & CPTO AlmaBetter

Vikas CTO

Related Tutorials to watch

view Allview-all

Top Articles toRead

view Allview-all
AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter