Category: Python

Python Program to Print Prime Numbers in a Given Interval

This Python program explains how you can print prime numbers in a given interval in python using loops. A number is a prime number if the following conditions are satisfied It is a Positive Number It is Greater than 1. Has no other factors except 1 and the number itself. Some of the examples of a prime number are 2,3 etc. How to Print Prime…

Logical Operators in Python

This tutorial will teach you about Python logical operators and how to use them to combine multiple conditions. You might want to verify several conditions at once. Use logical operators to accomplish this. The logical operators in Python are as follows: andornot and operator in Python The and operator determines if two conditions are True at the same time: If both conditions are true, it…

List in Python

This tutorial will teach you about the Python List type and how to effectively manipulate list elements. What is a List? A list is an ordered grouping of items. Python indicates a list with square brackets ([]). The following is an example of an empty list: A list typically contains one or more items. A comma is used to separate two items (,). As an…

Python Program to Display Hello world

This is a simple program written in Python language that displays Hello World. The programs demonstrates how the Python code looks like and is a beginner program. Python Program to Display Hello world Output This program uses the python’s built-in print function to display the string Hello, world in the screen. The strings in python are by default in either single quotes , double quotes…

for Loop with Range in Python

This article will teach you how to use the Python for loop to execute a code block a set number of times. In programming, it’s common to wish to run a block of code several times. A for loop is used to accomplish this. The syntax of a for loop is illustrated below: The index is referred to as a loop counter in this form….

Python Program to Swap Two Variables

In this Python program , you will learn how to swap two variables easily with 2 options. Swap two variable using a temporary variable in python. Swap two variable without using temporary variable in python. How to Swap two variables in Python without using Temporary Variable? Output a : 3b : 8 How to Swap Two variables without using Temporary variable in Python? There are…

Default Parameters in Python

This tutorial will teach you how to use Python’s default parameters to make function calls easier. You can give a default value for each argument when you define a function. The following syntax is used to specify default values for parameters: The assignment operator (=) is used to specify default values (value2, value3,…) for each parameter in this syntax. When you call a function and…

Python Program to Find the Square Root of Number

This python program will demonstrate how you can find the square root of a number using exponent operator python. Basic Program to find Square Root for Positive Number in Python Output The square root of 4.000 is 2.000 In this python program , we use the exponent operator ** to find the square root of the given positive number.

Python Program to Generate Multiplication Table

In this Python program, you will learn how to use loops in python to generate and display multiplication table of a given number. How to Generate and Display Multiplication Table in Python? Output 8 x 1 = 88 x 2 = 168 x 3 = 248 x 4 = 328 x 5 = 408 x 6 = 488 x 7 = 568 x 8 =…

Python Program to Check if a Number is Positive, Negative or Zero

In this python program, you’l learn how to check for a number of it is a positive , negative or zero. We will be using the conditional statements in python to solve this problem. How to find if a number is positive, negative or zero? Output The number that you entered is a Positive number A number is considered to be positive if it is…

Ternary Operator in Python

This tutorial will teach you about the Python ternary operator and how to use it to make your code more concise. The following program asks for your age and then calculates the ticket price: Here’s what happens if you type 18: If the age is more than or equal to 18, the following if…else line assigns 20 to the ticket price. Otherwise, the ticket price…

Strings in Python

This tutorial will teach you how to use the Python string and its fundamental operations. A character string is a collection of characters. Anything inside quotations is a string in Python. You have the option of using single or double quotations. For instance: If a string contains a single quotation, it should be enclosed in double quotes, as shown below: You can also use single…

Variables in Python

This tutorial will teach you about Python variables and how to effectively use them. When creating a program, you must manage a large number of values. Variables are used to store values. A variable in Python is a label to which you can assign a value. And every variable has a value associated with it. As an example: Output Hello, World! Good Bye! The variable…

Python Program to display "Hello, World!"

Problem Write a program in Python to display “Hello, World!” on the screen. Python Program to display “Hello, World!” in Console Window. This is a simple Python program that displays “Hello, World!” on the console window. Output Hello, World! The above Python program uses the print() function to display the Hello,World! string to the screen. Note that the strings are enclosed inside the single quotes…

Boolean Data Type in Python

This tutorial will teach you about the Python boolean data type, as well as falsy and truthy values. In programming, you frequently want to check whether a condition is true or false and then take action based on the outcome. Python provides the boolean data type to represent true and false. The technical name for a boolean value is bool. True and False are the…

sorted() function in Python

In this lesson, you’ll learn how to sort a list using the Python sorted() method. The sort() method sorts a list at its current location. To put it another way, it rearranges the components in the original list. The sorted() function is used to return the new sorted list from the original list: The original list is not changed by the sorted() method. Using the…

Python Program to Check Armstrong Number

In this Python program, you’ll learn how to check if the given number is a Armstrong number or not and display it on the console. How to Check if the Given Number is a Armstrong Number or Not in Python? A number is said to be an Armstrong number of it is a positive number and if abc… = an + bn + cn +…

Docstrings Functions in Python

This tutorial will teach you how to add documentation to a function using docstrings. Help() is a Python built-in function that displays a function’s documentation. The print() function documentation is demonstrated in the following example: The help() function can be used to display documentation for modules, classes, functions, and keywords. This tutorial only covers function documentation. Documenting functions with docstrings in Python You can use…

Comparison Operators in Python

This tutorial will teach you about Python comparison operators and how to use them to compare two values. In programming, you’ll frequently wish to compare one value to another. You use comparison operators to accomplish this. Python has six comparison operators: Less than ( < ) Less than or equal to (<=) Greater than (>) Greater than or equal to (>=) Equal to ( ==…

Python – How do list all files of a directory?

In Python ,you can list all files of a directory using the os.listdir(). How do list all files of a directory in Python? You can use the os.listdir() in pythpn which will list all the files that is in the directory includes files and directories. Incase you want just the files , you can filter it using the path function.