13. Python Lists - Quiz

1. Which of the following commands will correctly create a list in Python?
Answer: All of the mentioned
2. Suppose list1 is [2, 33, 222, 14, 25]. What is the result of list1[-1]?
Answer: 25
3. Which method is used to add a single element to the end of a list?
Answer: append()
4. What is the output when executing list("hello")?
Answer: ['h', 'e', 'l', 'l', 'o']
5. Which function returns the number of elements in a list?
Answer: len()
6. What is the output of the following code?

list1 = [1, 2, 3]
list2 = list1
list1[0] = 4
print(list2)
Answer: [4, 2, 3]
7. What is the primary difference between list.sort() and the sorted() function?
Answer: sort() modifies the list in place and returns None, while sorted() returns a new sorted list.
8. What will be the output of the following code?

numbers = [4, 2, 8, 1]
result = numbers.sort()
print(result)

Answer: None
9. Which parameter is used to sort a list in descending order?
Answer: reverse=True
10. Consider the following code snippet. What is the output?

words = ["apple", "banana", "kiwi", "cherry"]
words.sort(key=len)
print(words)

Answer: ['kiwi', 'apple', 'banana', 'cherry']
11. What is the output of the following code?

my_list = [10, 20, 30]
print(my_list.reverse())

Answer: None
12. Which of the following methods returns a new reversed list without modifying the original?
Answer: list[::-1]
Score: 0 / 0 answered

12. Python Lists - Sort(), Sorted & Reverse()

The following concise video provides practical demonstrations of Python’s sort(), sorted(), and reverse() methods to facilitate a quick and thorough understanding.

Sort - This method is specific to lists and modifies the original list directly.

Sorted - This function returns a new list containing the sorted elements. It is best for maintaining the original data while creating a sorted version.

Reverse - The most memory-efficient method which changes the list directly and does not return a new list. Since list.reverse() returns None, so you cannot assign its result to a new variable and expect a list value.

Table showing clear difference between Python Sort() & Python Sorted()


11. Python Lists - Append() & Insert()



This post covers the essential append() and insert() methods in Python. Both append() and insert() are primary methods in Python which are used to add elements to a list. In Python, append() is the standard and most efficient way to add a single element to the end of a list. Use insert() only when the element must be placed at a specific index.

10. Python Lists


A Python list can be called a collection of items in a particular order.

This short & Quick video tutorial outlines the fundamental operations involving Python lists, specifically focusing on how to retrieve entire lists, access individual elements using standard positive indexing, and utilize negative indexing for efficient access from the end of the sequence.

9. Python Data Types - Strings Quiz

1. Which of the following is not a valid way to create a string in Python?
Answer: char('A')
2. What will be the output of the following Python code?

text = "Hello World!"
result = text.upper()
print(result)
Answer: HELLO WORLD!
3. What will be the output of the following Python code?

text = "this is a test string."
output = text.capitalize()
print(output)
Answer: This is a test string.
4. Which of the following will produce the output "Hello World"?
Answer: All of the above.
5. What is the correct output of the following Python code?

print("Hello\nWorld")
Answer: Hello
World
6. What is the primary purpose of the strip() method in Python?
Answer: To remove leading and trailing whitespace or specified characters from a string.
7. What will be the output of the following Python code?

s = " Hello World "
print(s.strip())
Answer: "Hello World" (with leading and trailing spaces removed)
8. What will be the output of the following Python code?

s = "---Python---"
print(s.strip('-'))
Answer: "Python"
9. If s = "abcba", what will be the output of s.strip('ab')?
Answer: "c"
10. Which of the following methods removes only leading whitespace characters from a string?
Answer: lstrip()
Score: 0 / 0 answered

8. Python Data Types - Strings - Part 2



We already did a short video on Python data types - Strings. If you haven't watched that yet then please go and check it here. In this short video we will talk about tabs and new line characters in Python and also about Python Strip() function with an easy to understand example.

7. Python Data Types - Strings



In Python, a string is a fundamental data type used to represent sequences of characters, primarily for handling textual data. In this short video on Python Strings we are talking about string variables (like how to store strings in a variable), some string methods like Upper(), Lower(), Capitalize(), Title() etc. We also talk about string formatting, meaning embedding values into strings using f-strings or the format() method.

6. Python Data Types - Numbers Quiz

1. What is the data type of the variable x in the following code: x = 5?
Answer: Integer
2. Which of the following is a valid way to represent a floating-point number in Python?
Answer: 5.0
3. Which of the following is a complex number in Python?
Answer: 5 + 3j
4. What is the data type of the result of the expression 5 ** 2?
Answer: Integer
5. Which of the following expressions will result in a floating-point number?
Answer: 5 / 2
6. What is the result of 5 ** 2?
Answer: 25
7. What is the data type of the variable x in x = 5.5?
Answer: Float
8. What is the result of 10 % 3?
Answer: 1
9. What will be the values of a, b, and c after executing the code: a, b, c = [1, 2, 3]?
Answer: a = 1, b = 2, c = 3
10. What will be the output of type(5.5)?
Answer: <class 'float'>
Score: 0 / 0 answered

5. Python Data Types - Numbers



In this short video on Python data types, we will talk about data type Numbers and within numbers mainly integers and floats. Once you are done with the video you can check your knowledge with this quiz on Python data types numbers.

3. Python Variables

This short video talks about Python variables in general, are they case-sensitive?, latest value stored in a Python variable, valid variable names, Python variable examples and so on. When you are done with the video, you can test your knowledge on Python variables by taking this quiz.

4. Python Variables Quiz

1. What is the correct way to assign an integer value 10 to a variable named my_number in Python?
Answer: my_number = 10
2. Is Python case-sensitive when it comes to variable names?
Answer: Yes
3. What happens if you try to use a variable that has not been assigned a value?
Answer: Python will raise a NameError.
4. Which of the following is an invalid variable name?
Answer: 1st_var
5. Which of the following statements about Python variable names is true?
Answer: Variable names can be of any length.
6. By convention, how do programmers indicate that a variable should be treated as a constant?
Answer: By writing the variable name entirely in uppercase.
7. Consider the following code. What will be the output?
x = 5
x = "Hello"
print(x)

Answer: Hello
8. What is the output of the following code?
x = 10
y = x
x = 20
print(y)
Answer: 10
9. What is the purpose of "del" statement in Python?
Answer: to delete a variable
10. What is a variable in Python?
Answer: A container to store values
Score: 0 / 0 answered

2. Running First Python Code

In the previous post we installed Python on Windows, wrote our very first Python code and ran it from Command prompt. We also checked Python version installed from Command prompt.

This 2.24 minutes video is again a quick short introduction to downloading and installing Python, installing Visual Studio Code and Python extension for Visual Studio Code. We also run our first Python program in Visual Studio Code in this video.

1. Installing Python and running Hello World

Download Python and install it on your system. I am working on Windows so installed it on Windows.
Downlod link for Python.
Few screenshots taken during installation:
installing Python

installing Python

Once Python is installed successfully, open Notepad and type:
print ('Hello World')
installing Python

Save the above Notepad file with .py extension. For example I saved it as 'firstcode.py'.
Open Command prompt and go to the location (I saved it on my desktop) where firstcode.py is saved on your system. To run the code, type py [NAME_OF_THE_FILE] and hit Enter.
installing Python

Few points to consider:
1. The code we wrote is calling built-in function called 'print' and NOT 'Print'. No capital p, else it will show an error like:
installing Python

2. To know the version of the Python installed, type py on the Command prompt and press Enter.
installing Python

To go to the Command prompt again, type quit or (exit()) and hit Enter.
installing Python

3. Above, we wrote the Python code in file and ran that. You can also wirte the Python snippet directly in the terminal window like:
installing Python

As you can see in the screenshot above, we first went to Python prompt by typing py and then wrote the snippet which prints Hello World and then hit Enter.