You are developing a Python application where you need to store configuration settings. Which data type would you use to store key-value pairs of configuration settings?
- Dictionaries
- Lists
- Sets
- Tuples
In Python, dictionaries are used to store key-value pairs, making them an excellent choice for storing configuration settings where you can easily access values by their keys. Lists and tuples are used for ordered collections of items, and sets are used for storing unique elements.
You are given a task to parse string dates in various formats and convert them to datetime objects in Python. Which module would you use to achieve this?
- calendar
- datetime
- dateutil
- time
In Python, the datetime module provides classes for working with dates and times, making it the appropriate choice for parsing string dates into datetime objects. The time module deals with lower-level time-related operations, and the calendar module is used for calendar-related calculations. The dateutil module is a third-party library that enhances the capabilities of datetime.
To represent binary data in Python, you would use the ____ data type.
- binary
- binary_data
- bit
- bytes
In Python, the bytes data type is used to represent binary data. It is a sequence of bytes, which is often used for working with binary files, network protocols, and other low-level data operations.
The ____ built-in function is used to convert a variable to an integer data type, if possible.
- convert()
- int()
- parse()
- str2int()
The int() built-in function in Python is used to convert a variable to an integer data type. If the variable can be converted to an integer, it will do so; otherwise, it will raise a ValueError if the conversion is not possible.
In Python, the ____ method is used to get the length of a string.
- count()
- len()
- size()
- str_len()
In Python, the len() method is used to get the length (number of characters) of a string. It is a built-in function that works with strings, lists, tuples, and other iterable objects.
The ____ keyword is used to create a new variable and assign it a specific data type.
- define
- int
- type
- var
In Python, the type keyword is used to create a new variable and assign it a specific data type. For example, x = type(5) would assign the integer type to the variable x.
To concatenate two strings in Python, you can use the ____ operator.
- -
- *
- /
- +
In Python, you can concatenate two strings using the + operator. For example, "Hello, " + "world" would result in "Hello, world".
In Python, a ____ is a built-in data type used to store multiple items in a single variable.
- dictionary
- list
- string
- tuple
In Python, a list is a built-in data type that is used to store multiple items in a single variable. Lists are ordered and mutable, making them suitable for a wide range of applications.
How can you create a new instance of a custom class and assign it to a variable in Python?
- class.new()
- MyClass()
- MyClass.new()
- new MyClass()
In Python, you create a new instance of a custom class using the class name followed by parentheses. This creates a new object of the class and can be assigned to a variable.
What is the difference between is and == when comparing variables in Python?
- is compares object identity, checking if two variables refer to the same object. == compares object values, checking if two variables have the same content.
- is compares object values, checking if two variables have the same content. == compares object identity, checking if two variables refer to the same object.
- is is used for deep equality comparisons, while == is used for shallow equality comparisons.
- is is used for strict equality comparisons, while == is used for loose equality comparisons.
This means that is
checks if both variables point to the same memory location, while ==
checks if the values of the objects are equal.