Category: Python
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…