Does Python have a ternary operator? Yes it does!
In this tutorial you will learn everything about it so you can use it in your programs.
The Python ternary operator (or conditional expression) works on three operands and it allows to write the logic of an if else statement in a single line of code. With the ternary operator you specify an expression evaluated if the condition is True, the condition itself and an expression evaluated if the condition is False.
Don’t worry if it’s not super clear from the definition, we will look at a few examples that will…
Have you seen the syntax error “unexpected EOF while parsing” when you run a Python program? Are you looking for a fix? You are in the right place.
The error “unexpected EOF while parsing” occurs when the interpreter reaches the end of a Python file before every code block is complete. This can happen, for example, if any of the following is not present: the body of a loop (for / while), the code inside an if else statement, the body of a function.
We will go through few examples that show when the “unexpected EOF while parsing” error occurs…
Have you ever heard about the list comprehension in Python? It simplifies the way you work with lists and it makes your code more concise.
The list comprehension is a Python construct that reduces the lines of code required to generate a new list or to filter an existing list. A list comprehension is enclosed within square brackets and it’s made of an expression, one or more for loops and an optional condition to filter the list generated.
We will first define the list comprehension and then go through a series of examples that will make this part of your…
If you are getting started with Python one of the topics you will encounter soon are functions. Here is how you can start using them.
Python functions allow to make your code more readable and reusable. A function is a block of code that performs a specific operation or returns a specific result. Once you define a function you can simply call it over and over in your code without having to rewrite that code again.
We will start by looking at how to define a function and then section after section we will keep building on the concepts you…
Are you considering learning coding and are you thinking about Python as a language? In this article I will answer some of the burning questions you might have.
Learning Python coding is one of the top skills in today’s IT industry. That’s because the Python programming language can be used for several purposes. You can use it to create web applications, APIs, standalone applications, artificial intelligence (AI) applications, scripts, videogames and much more.
Do you want to be able to start coding fast and build a skill that will make you valuable in today’s market? It’s time to learn Python.
The tuple is a basic Python data type. It’s important to understand it and to know how to use it. This tutorial will help you with that.
Python tuples are used to store an ordered sequence of values. Tuples are immutable, this means that the values in a tuple cannot be changed once the tuple is defined. The values in a tuple are comma-separated and they are surrounded by parentheses.
You will see some examples that clarify how you can use tuples in your programs and why.
Let’s start exploring the world of tuples!
To create a new tuple you…
If you haven’t used the Python zip() function yet this is an opportunity to look at how it works and how you can use it in your programs.
The Python zip() function takes as input multiple iterables and returns an iterator of tuples where the n-th tuple contains the n-th element from every iterable. The zip function can also be applied to iterable of different length and in that case it stops when the smallest iterable is exhausted.
We will go through some example to see how to apply the zip function to multiple Python data types.
Let’s zip them…
Printing even and odd numbers from a Python list is a good exercise if you are getting started with Python and you want to learn its basics.
To extract even and odd numbers from a Python list, you can use a for loop and the Python modulo operator. A second option is to replace the for loop with a list comprehension. The extended syntax of the slice operator also allows us to do that with one line of code but only when you have lists of consecutive numbers.
We will go through few examples in this tutorial and the point…
In your Python program you might want to validate an IP address. This can be a requirement if you write OS level programs and not only.
To validate an IP address using Python you can use the ip_address() function of the ipaddress module. This works both for IPv4 and IPv6 addresses. You can also validate an IP address by using a custom function or a regular expression that verify the sets of numbers an IP address is made of.
Let the validation begin!
The simplest way to validate if a string represents an IP address is by using the Python…
Getting every other element from a Python list might seem easy to do but it actually requires some knowledge of Python basics.
To get every other element in a Python list you can use the slice operator and use the fact that this operator allows providing a step argument (with its extended syntax). Another approach is to use the mathematical concept of the remainder and apply it to list indexes.
Let’s get creative and come up with multiple ways to solve this problem!
Firstly, let’s create a list of strings…
>>> countries = ['Italy', 'United Kingdom', 'Germany', 'United States', 'France']