Found 26504 Articles for Server Side Programming

How to declare a global variable in Python?

pawandeep
Updated on 11-Mar-2021 09:06:20

4K+ Views

What is a global variable?A global variable is a variable that is declared outside the function but we need to use it inside the function.Example Live Demodef func():    print(a) a=10 func()Output10Here, variable a is global. As it is declared outside the function and can be used inside the function as well. Hence the scope of variable a is global.We will see what happens if we create a variable of same name as global variable inside the function.In the above example, the variable a is declared outside the function and hence is global.If we declare another variable with same name inside ... Read More

Cummulative Nested Tuple Column Product in Python

AmitDiwan
Updated on 11-Mar-2021 09:10:18

225 Views

If it is required to find the cumulative column product of a nested tuple, the 'zip' method and a nested generator expression can be used.Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.Below is a demonstration of the same −ExampleLive Demotuple_1 = ((11, 23), (41, 25), (22, 19)) tuple_2 = ((60, 73), (31, 91), ... Read More

Kth Column Product in Tuple List in Python

AmitDiwan
Updated on 11-Mar-2021 09:05:42

225 Views

When it is required to find the 'K'th column product in a list of tuple, a simple list comprehension and a loop can be used.A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.The list comprehension ... Read More

Remove nested records from tuple in Python

AmitDiwan
Updated on 11-Mar-2021 09:05:14

351 Views

When it is required to remove nested record/tuples from a tuple of tuple, a simple loop and the 'isinstance' method and the enumerate method can be used.The enumerate method adds a counter to the given iterable, and returns it. The 'isinstance' method checks to see if a given parameter belong to a specific data type or not.Below is a demonstration of the same −ExampleLive Demotuple_1 = (11, 23, (41, 25, 22), 19) print("The tuple is : ") print(tuple_1) my_result = tuple() for count, elem in enumerate(tuple_1):    if not isinstance(elem, tuple):       my_result = my_result + ... Read More

Write a program to form a cumulative sum list in Python

pawandeep
Updated on 10-Mar-2021 14:26:20

3K+ Views

The cumulative sum till ith element refers to the total sum from 0th to ith element.The program statement is to form a new list from a given list. The ith element in the new list will be the cumulative sum from 0 to ith element in the given list.For example, Input[10, 20, 30, 40, 50]Output[10, 30, 60, 100, 150]Input[1, 2, 3, 4, 5]Output[1, 3, 6, 10, 15]Following is a program to form a cumulative sum list using the input list −The input list is passed to the function cumSum() which returns the cumulative sum list.We declare an empty list cum_list ... Read More

Palindrome in Python: How to check a number is palindrome?

pawandeep
Updated on 11-Mar-2021 12:25:51

705 Views

What is a palindrome?A palindrome is a string that is the same when read from left to right or from right to left. In other words, a palindrome string is the one whose reverse is equal to the original string.For example, civic, madam are palindromes.Cat is not a palindrome. Since its reverse is tac, which is not equal to the original string (cat).Write a program to find whether the input string is a palindrome or not.Method 1 - Find Reverse of the stringThe main thing required in the program is to find the reverse of the string.The reverse can be ... Read More

How to round off a number in Python?

pawandeep
Updated on 10-Mar-2021 14:18:23

861 Views

Python has an inbuilt round() function to round off a number.The round() method in Python takes two parameters −The first one is the number to be rounded off.The second one specifies the number of digits to which the number must be rounded off.Here, the second parameter is optional.If second parameter is not specified, then the round() method returns the integer using floor() and ceil().It will look for the digits after the decimal.If those are less than 5, it returns the floor() of the number passed.Whereas if the digits after decimal are greater than 5, it returns the ceil() of the ... Read More

How to print pattern in Python?

pawandeep
Updated on 10-Mar-2021 14:15:23

2K+ Views

Patterns in Python can be printed using nested for loops. The outer loop is used to iterate through the number of rows whereas the inner loop is used to handle the number of columns. The print statement is modified to form various patterns according to the requirement.The patterns can be star patterns, number patterns, alphabet patterns. The patterns can be of different shapes, triangle, pyramids, etc.ExampleAll these patterns can be printed with the help of for loops with modified print statements which forms these different patterns.The basic idea between the printing of these patterns is the same with slight differences.We ... Read More

How to reverse a number in Python?

pawandeep
Updated on 10-Mar-2021 14:12:28

2K+ Views

Reversing an integer number is an easy task. We may encounter certain scenarios where it will be required to reverse a number.Input: 12345 Output: 54321There are two ways, we can reverse a number −Convert the number into a string, reverse the string and reconvert it into an integerReverse mathematically without converting into a stringConvert into string and ReverseThis method of reversing a number is easy and doesn’t require any logic. We will simply convert the number into string and reverse it and then reconvert the reversed string into integer. We can use any suitable method for reversing the string.Example Live Demodef ... Read More

How to concatenate two strings in Python?

pawandeep
Updated on 24-Aug-2023 16:21:09

37K+ Views

Concatenating two strings refers to the merging of both the strings together. Concatenation of “Tutorials” and “Point” will result in “TutorialsPoint”.We will be discussing different methods of concatenating two strings in Python.Using '+' operatorTwo strings can be concatenated in Python by simply using the '+' operator between them.More than two strings can be concatenated using '+' operator.Example Live Demos1="Tutorials" s2="Point" s3=s1+s2 s4=s1+" "+s2 print(s3) print(s4)OutputTutorialsPoint Tutorials PointUsing % operatorWe can use the % operator to combine two string in Python. Its implementation is shown in the below example.Example Live Demos1="Tutorials" s2="Point" s3="%s %s"%(s1, s2) s4="%s%s"%(s1, s2) print(s3) print(s4)OutputTutorials Point TutorialsPointThe %s denotes ... Read More

Advertisements