×


Category: WordPress


Strip() Function in Python

This article covers how to use the strip() function in Python. In fact, the Python strip() method removes any spaces or specified characters at the start and end of a string. strip() returns a new string without the characters you have specified to remove.

The syntax for the strip() method is:

" TEST ".strip()


Python String Lower() and Upper()

This article covers how to use the lower() and upper() functions in Python. In fact, the upper() method converts all lowercase characters in a string into uppercase characters and returns it while the lower() method converts all uppercase characters in a string into lowercase characters and returns it.


Python If Else Statement - Explained with Examples

This article covers how to use the if else statement in Python. In fact, An if else Python statement evaluates whether an expression is true or false. If a condition is true, the "if" statement executes. Otherwise, the "else" statement executes. Python if else statements help coders control the flow of their programs.


Python Conditions and If statements

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b


Python Nested if statements

We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.


Python Nested if Example

'''In this program, we input a number

check if the number is positive or

negative or zero and display

an appropriate message

This time we use nested if statement'''


num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Output 1 will give:

Enter a number: 5
Positive number


Output 2 will give:

Enter a number: -1
Negative number


Output 3 will give:

Enter a number: 0
Zero


Python Arrays in Linux - Explained with examples

This article covers how to use Array in Python. In fact, An array is a special variable, which can hold more than one value at a time.


Python Lists Vs Arrays

In Python, we can treat lists as arrays. However, we cannot constrain the type of elements stored in a list. 

For example:

# elements of different types
a = [1, 3.5, "Hello"] 
If you create arrays using the array module, all elements of the array must be of the same numeric type.
import array as arr
# Error
a = arr.array('d', [1, 3.5, "Hello"])

Output will give:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
    a = arr.array('d', [1, 3.5, "Hello"])
TypeError: must be real number, not str


Python float() Function in Linux - Step by step guide ?

This article covers how to use the float() function in Python. In fact, the float() method returns a floating point number from a number or a string. There are two number types in Python: floating-point numbers (floats) and integers. While floats can contain decimals, integers cannot.


float() Return Value

The float() method returns:

  • Equivalent floating point number if an argument is passed.
  • 0.0 if no arguments passed.
  • OverflowError exception if the argument is outside the range of Python float.


Python pow() Function in Linux - Step by step Guide ?

This article covers how to use the Python pow() function. In fact, the pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z.