What is the correct way to create a list with type float?

Top 70 Python Multiple Choice Questions &Answers

Let’s begin!

1. Do we need to compile a program before execution in Python?

  • Yes
  • No

2. How to output the string “May the odds favor you” in Python?

  • print(“May the odds favor you”)
  • echo(“May the odds favor you”)
  • System.out(“May the odds favor you”)
  • printf(“May the odds favor you”)

3. How to create a variable in Python with a value 22.6?

  • int a = 22.6
  • a = 22.6
  • Integer a = 22.6
  • None of the above

4. How to add a single-line comment in Python?

  • /* This is comment */
  • !! This is comment
  • // This is comment
  • # This is comment

5. How to represent 261500000 as a floating number in Python?

  • 2.615E8
  • 261500000
  • 261.5E8
  • 2.6

6. Select the correct example of complex datatype in Python

  • 3 + 2j
  • -100j
  • 5j
  • All of the above are correct

7. What is the correct way of creating a multi-line string in Python?

  • str = “”My name is Kevin and I
    live in New York””
  • str = “””My name is Kevin and I
    live in New York”””
  • str = “My name is Kevin and I
    live in New York”
  • All of the above

8. How to convert the uppercase letters in the string to lowercase in Python?

  • lowercase()
  • capitalize()
  • lower()
  • toLower()

9. How to access substring “Kevin” from the following string declaration in Python:

1
2
3
str = "My name is Kevin"
  • str[11:16]
  • str(11:16)
  • str[11][16]
  • str[11-16]

10. Which of the following is the correct way to access a specific character. Let’s say we need to access the character “K” from the following string in Python?

1
2
3
str = "My name is Kevin"
  • str(11)
  • str[11]
  • str:9
  • None of the above

11. Which Python module is used to parse dates in almost any string format?

  • datetime module
  • time module
  • calendar module
  • dateutil module

12. Which of the following is the correct way to indicate Hexadecimal Notation in Python?

  • str = ‘\62’
  • str = ’62’
  • str = “62”
  • str = ‘\x62’

13. To begin slicing from the end of the string, which of the following is used in Python?

  • Indexing
  • Negative Indexing
  • Begin with 0th index
  • Escape Characters

14. How to fetch characters from a given range in Python?

  • [:]
  • in operator
  • []
  • None of the above

15. How to capitalize only the first letter of a sentence in Python?

  • uppercase() method
  • capitalize() method
  • upper() method
  • None of the above

16. What is the correct way to get maximum value from Tuple in Python?

  • print (max(mytuple));
  • print (maximum(mytuple));
  • print (mytuple.max());
  • print (mytuple.maximum);

17. How to fetch and display only the keys of a Dictionary in Python?

  • print(mystock.keys())
  • print(mystock.key())
  • print(keys(mystock))
  • print(key(mystock))

18. How to align a string centrally in Python?

  • align() method
  • center() method
  • fill() method
  • None of the above

19. How to access value for key “Price” in the following Python Dictionary:

1
2
3
4
5
6
7
8
mystock = {
"Product": "Earphone",
"Price": 800,
"Quantity": 50,
"InStock" : "Yes"
}
  • mystock[“Product”]
  • mystock(“Product”)
  • mystock[Product]
  • mystock(Product)

20. How to set the tab size to 6 in Python Strings?

  • expandtabs(6)
  • tabs(6)
  • expand(6)
  • None of the above

21. ___________ uses square brackets for comma-separated values in Python? Fill in the blanks with a Python collection?

  • Lists
  • Dictionary
  • Tuples
  • None of the above

22. Are Python Tuples faster than Lists?

  • TRUE
  • FALSE

23. How to find the index of the first occurrence of a specific value “i”, from string “This is my website”?

  • str.find(“i”)
  • str.find(i)
  • str.index()
  • str.index(“i”)

24. How to display whether the date is AM/PM in Python?

  • Use the %H format code
  • Use the %p format code
  • Use the %y format code
  • Use the %I format code

25. Which of the following is the correct way to access a specific element from a Multi-Dimensional List?

  • list[row_size:column_size]
  • list[row_size][column_size]
  • list[(row_size)(column_size)]
  • None of the above

26. Which of the following Bitwise operators in Python shifts the left operand value to the right, by the number of bits in the right operand. The rightmost bits while shifting fall off?

  • Bitwise XOR Operator
  • Bitwise Right Shift Operator
  • Bitwise Left Shift Operator
  • None of the Above

27. How to specify range of index in Python Tuples? Let’s say our tuple name is “mytuple”

  • print(mytuple[1:4])
  • print(mytuple[1 to 4])
  • print(mytuple[1-4])
  • print(mytuple[].slice[1:4])

28. How to correctly create a function in Python?

  • demoFunction()
  • def demoFunction()
  • function demoFunction()
  • void demoFunction()

29. Which one of the following is a valid identifier declaration in Python?

  • str = “demo666”
  • str = “666”
  • str = “demo demo2”
  • str = “$$$666”

30. How to check whether all the characters in a string is printable?

  • print() method
  • printable() method
  • isprintable() method
  • echo() method

31. How to delete an element in a Dictionary with a specific key. Let’s say we need to delete the key “Price”

  • del dict[‘Price’]
  • del.dict[‘Price’]
  • del dict(‘Price’)
  • del.dict[‘Price’]

32. How to swap cases in Python i.e. lowercase to uppercase and vice versa?

  • casefold() method
  • swapcase() method
  • case() method
  • title() method

33. The def keyword is used in Python to _______ ?

  • Define a function name
  • Define a list
  • Define an array
  • Define a Dictionary

34. Which of the following is used to empty the Dictionary in Python? Let’s say our dictionary name is “mystock”.

  • mystock.del()
  • clear mystock
  • del mystock
  • mystock.clear()

35. How to display only the day number of year in Python?

  • date.strftime(“%j”)
  • date.strftime(“%H”)
  • date.strftime(“%I”)
  • date.strftime(“%p”)

36. What is used in Python functions, if you have no idea about the number of arguments to be passed?

  • Keyword Arguments
  • Default Arguments
  • Required Arguments
  • Arbitrary Arguments

37. What is the correct way to create a Dictionary in Python?

  • mystock = {“Product”: “Earphone”, “Price”: 800, “Quantity”: 50}
  • mystock = {“Product”- “Earphone”, “Price”-800, “Quantity”-50}
  • mystock = {Product[Earphone], Price[800], Quantity[50]}
  • None of the above

38. In Python Functions, what is to be used to skip arguments or even pass them in any order while calling a function?

  • Keyword arguments
  • Default Arguments
  • Required Arguments
  • Arbitrary Arguments

39. How to get the total length of Tuple in Python?

  • count() method
  • length() method
  • len() method
  • None of the above

40. How to access a value in Tuple?

  • mytuple(1)
  • mytuple{1}
  • mytuple[1]
  • None of the above

41. What is to be used to create a Dictionary of arguments in a Python function?

  • Arbitrary arguments in Python (*args)
  • Arbitrary keyword arguments in Python (**args)
  • Keyword Arguments
  • Default Arguments

42. How to convert the lowercase letters in the string to uppercase in Python?

  • upper()
  • uppercase()
  • capitalize()
  • toUpper()

43. What is the correct way to get minimum value from a List in Python?

  • print (minimum(mylist));
  • print (min(mylist));
  • print (mylist.min());
  • print (mylist.minimum());

44. Is using a return statement optional in a Python Function?

  • Yes
  • No

45. Which of the following correctly converts int to float in Python?

  • res = float(10)
  • res = convertToFloat(10)
  • None of the above

46. How to get the type of a variable in Python?

  • print(typeOf(a))
  • print(typeof(a))
  • print(type(a))
  • None of the above

47. How to compare two operands in Python and check for equality? Which operator is to be used?

  • =
  • in operator
  • is operator
  • == (Answer)

48. What is the correct way to get minimum value from Tuple?

  • print (min(mytuple));
  • print (minimum(mytuple));
  • print (mytuple.min());
  • print (mytuple.minimum);

49. How to display only the current month’s number in Python?

  • date.strftime(“%H”)
  • date.strftime(“%I”)
  • date.strftime(“%p”)
  • date.strftime(“%m”)

50. How to compare two objects and check whether they have the same memory locations?

  • is operator
  • in operator
  • **
  • Bitwise operators

51. How to fetch and display only the values of a Dictionary in Python?

  • print(mystock.value())
  • print(mystock.values())
  • print(values(mystock))
  • print(value(mystock))

52. Which operator is used in Python to raise numbers to the power?

  • Bitwise Operators
  • Exponentiation Operator (**)
  • Identity Operator (is)
  • Membership Operators (in)

53. Can we create a Tuple in Python without parenthesis?

  • TRUE
  • FALSE

54. ____________ represents key-value pair in Python?

  • Tuples
  • Lists
  • Dictionary
  • All the Above

55. Which of the following Bitwise operators sets each bit to 1, if only one of them is 1, i.e. if only one of the two operands are 1.

  • Bitwise XOR
  • Bitwise OR
  • Bitwise AND
  • Bitwise NOT

56. What is the correct way to get maximum value from a List in Python?

  • print (maximum(mylist));
  • print (mylist.max());
  • print (max(mylist));
  • print (mylist.maximum());

57. An example to correctly begin searching from the end range of index in Python Tuples. Let’s say our Python Tuple has 4 elements

  • print(mytuple[-3 to -1])
  • print(mytuple[3-1])
  • print(mytuple[].slice[-3:-1])
  • print(mytuple[-3:-1])

58. Which of the following Bitwise operators in Python shifts the left operand value to the left, by the number of bits in the right operand. The leftmost bits while shifting fall off.

  • Bitwise XOR Operator
  • Bitwise Right Shift Operator
  • Bitwise Left Shift Operator
  • None of the Above

59. Can we update Tuples or any of its elements in Python after the assignment?

  • Yes
  • No

60. How to display only the current month’s name in Python?

  • date.strftime(“%H”)
  • date.strftime(“%B”)
  • date.strftime(“%m”)
  • date.strftime(“%d”)

61. ___________ uses Parenthesis for comma-separated values in Python? Fill in the blanks with a Python collection?

  • List
  • Tuples
  • None of the above

62. How to create an empty Tuple in Python?

  • mytuple = ()
  • mytuple = (0)
  • mytuple = 0
  • mytuple =

63. Can we have duplicate keys in Python Dictionary?

  • TRUE
  • FALSE

64. Lists in Python are Immutable?

  • TRUE
  • FALSE

65. What is the correct way to create a list with type float?

  • mylist = [5.7, 8.2, 9. 3.8, 2.9]
  • mylist = (5.7, 8.2, 9. 3.8, 2.9)
  • mylist = {5.7, 8.2, 9. 3.8, 2.9}
  • None of the above

66. How to display only the day number of year in Python?

  • date.strftime(“%j”)
  • date.strftime(“%H”)
  • date.strftime(“%I”)
  • date.strftime(“%p”)

67. Delete multiple elements in a range from a Python List

  • del mylist[2:4]
  • del mylist(2:4)
  • del mylist[2 to 4]
  • del mylist(2 to 4)

68. How to fetch last element from a Python List with negative indexing?

  • print(“Last element = “,mylist[0])
  • print(“Last element = “,mylist[])
  • print(“Last element = “,mylist[-1])
  • None of the above

69. What is the correct way to get the length of a List in Python?

  • mylist.count()
  • count(mylist)
  • len(mylist)
  • length(mylist)

70. To get today’s date, which Python module is to be imported?

  • calendar module
  • datetime module
  • dateutil module
  • None of the above

We saw the top 70 Multiple Choice Questions and Answers above. If you liked this article, then kindly share with others.

Recommended Posts

  • Java Interview Questions & Answers
  • Android Interview Questions & Answers
  • ReactJS Interview Questions & Answers
  • C programming Quiz
  • C++ Quiz
  • Android Quiz
  • jQuery Quiz
  • HTML5 Quiz
  • CSS Quiz
  • PHP Quiz
Functions in Python with Examples
How to Install Python on Windows 10

What is a python list

  • The Group of elements is called a list in python.
  • Python lists are very similar to an array but the main difference is, an array can store only one type of element whereas, a list can store different types of elements.
  • List are mutable (changeable).
  • Lists are dynamics which means size is not fixed. Lists are represented using a square bracket[ ], separated by commas.

Example:

x = ['Lists',6.3,2.2,12,32] print(x)

After writing the above code (python list), Ones you will print“x”then the output will appear as a“[‘List’,6.3,2.2,12,32]”. You can refer to the below screenshot for creating a List with different data types.

python list

This is how to create a list in Python.

You may like Python program to reverse a string with examples.

Create a list in python with size

Here, We will see how to create a python list with a fixed size. I have initialized the element array with some size.

Example:

size = [0]*5 print(size)

After writing the above code (list in python with size), Ones you will print“size”then the output will appear as a“[0, 0, 0, 0, 0]”. Here, I have initialized it with [0] and my fixed-size value is 5. You can refer to the below screenshot for creating a List with a fixed size.

Create a list in python with size

Method 1: List Comprehension

Suppose we have a list:

a = ["1", "2.0", "-3.4"]

Now, check the type of the first list element:

print(type(a[0])) # <class 'str'>

Let’s apply the built-in function float(), and get a list of floats using list comprehension:

print([float(x) for x in a]) # [1.0, 2.0, -3.4]

?List comprehension is a compact way of creating lists. The simple formula is [expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of for and if statements.

You can watch me explain list comprehensions in this video:

Check the type of numbers in the new list:

A = [float(x) for x in a] print(type(A[0])) # <class 'float'>

The built-in function float() converts a string to a float. Thus, it helps us create a new list of floats from the list of strings in a single line of code.

Basic Data Types in Python

by John Sturtz basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Basic Data Types in Python

Now you know how to interact with the Python interpreter and execute Python code. It’s time to dig into the Python language. First up is a discussion of the basic data types that are built into Python.

Here’s what you’ll learn in this tutorial:

  • You’ll learn about several basic numeric, string, and Boolean types that are built into Python. By the end of this tutorial, you’ll be familiar with what objects of these types look like, and how to represent them.
  • You’ll also get an overview of Python’s built-in functions. These are pre-written chunks of code you can call to do useful things. You have already seen the built-in print() function, but there are many others.

Free PDF Download: Python 3 Cheat Sheet

Take the Quiz: Test your knowledge with our interactive “Basic Data Types in Python” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Python List

In this tutorial, we'll learn everything about Python lists: creating lists, changing list elements, removing elements, and other list operations with the help of examples.

Video liên quan

Postingan terbaru

LIHAT SEMUA