Contrary to other programming languages, such as C++ or
Java, you don’t specify the data type when defining a variable in Python, but
it doesn’t mean that the variable doesn’t have a type.
You can check the variable type using the type function that returns the type of
If you add a dictionary to a list in Python, it won’t create
a copy of this dictionary, but rather add a reference to the dictionary.
If you create a variable, you can easily convert it to a string using the print function.
sys.maxint is the
maximum value for the int variable in Python 2.
Python 3 doesn’t use this property because the int type in
Python 3 has no limit. For Python 3, you can use sys.maxsize to check the maximum size lists, strings, dicts, and
many other containers can have.
If you want to create an empty array (or list because there are no arrays in Python), you can use this line of code:
To convert string to time in Python 3 follow these steps:
- Import the time module.
- Assign the result of the time function to a
- Format the value with the strptime function.
- Specify whether you want to display 12 or 24-hour
If you are asking whether there is a null object is Python, the answer is: no, there isn’t. But it
doesn’t mean you can’t assign a variable with no value.
You have a list, and you want to check whether an element
you have is already present inside the list. There are a few ways you can do it
The quickest way to check whether a character in a string is a letter is to use the str.isalpha() function.
function takes a float value (which represents the number of seconds) as an
argument and causes a program to wait for a given number of seconds. After that
time the next part of the code is going to be executed.
To get indexes of min and max values in a list, first, you have to use the min and max functions to find the smallest and largest numbers. Next, use the index function of the list to find out the index of these numbers.
To create a datetime object, the datetime module has to be imported. Next, date and time have to be added as arguments.
To install a wheel package from the Python Package Index site (PyPI), open the console and run a package manager called PIP.
In Python, lists can be used as arrays, because the language doesn’t have built-in support. An array is one chunk of memory that usually consists of multiple elements, where a list is a list of objects where one element points to another and it doesn’t have to be inside one block of memory.
After you install Python on your
computer, you can run scripts in a few different ways. One of these ways is
terminal. In this lesson, I’ll show you how to do it.
With PIP, you can list all module versions with the following command:
The quickest way to check the Python version is to run this command in the command line:
The process of importing a function from a file in Python is
similar to importing modules. You have to create two files. Next, insert the
definition of the function in one file and call the function from another.
To convert an integer to string, Python uses the str() function. This function takes one
argument (e.g. integer) and converts it into a string. This value can be
displayed as an output or merged with other string values.
You can compare two datetime objects using standard
comparison operators (<, >, ==, !=). Before using the datetime class you
have to import the datetime module.