Identity Operators in Python

These operators are used to check if two objects are at the same memory location. Notice that they do not compare values, but memory location. They are: is: returns True if both objects are identical is not: returns True if…

Comparison Operators in Python

Use comparison operators to compare two values. These operators return either True or False. They are: ==: Equal !=: Not equal >: Greater than <: Less than >=: Greater than or equal to <=: Less than or equal to Let’s…

Logical Operators in Python

Logical operators are used to combine statements applying boolean algebra as shown in this article Booleans in Python. They are: and: True only when both statements are true or: False only when both x and y are false not: The…

Assignment Operators in Python

As the name implies, these operators are used to assign values to variables. x = 7 in the first example is a direct assignment storing the number 7 in the variable x. The assignment operation takes the value on the…

Arithmetic Operators in Python

Arithmetic operators are the most common type of operators and also the most recognizable ones. They allow you to perform mathematical operations. They are: +: Addition -: Subtraction *: Multiplication /: Division **: Exponentiation //: Floor Division, rounds down the…

zfill(): add zeros to a string in Python

Use the zfill() to insert zeros 0 at the beginning of a string. The amount of zeros is given by the number passed as argument minus the number of chars in the string. The word ‘beach’ has 5 characters, which…

center(): centered string in Python

Use the center() method to center a string. word = 'beach' number_spaces = 32 word_centered = word.center(number_spaces) print(word) #'beach' print(word_centered) ##output: ' beach ' Notice the spaces in the second string. The word ‘beach’ has 5 characters, which gives us…

Bitwise Operators in Python

Bitwise Operators allow you to perform operations on binary numbers. The values are automatically converted to binary and then the logic is applied to them. The output is also converted back from binary implicitly. They are: &: AND Only the…

Check if number is even or odd in Python

Let’s see a program to check if a number is either even or odd. The logic is simple: take a number and divide it by 2, if the remainder is 0, then the number is even, otherwise the number is…

How to find the largest number in Python

In this post, we will learn how to find the largest number. The first program uses the if statement to achieve that. The second program, the recommended way, uses the built-in max function and a list. Code using the if…