Found 34490 Articles for Programming

How to generate byte code file in python

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

All python program automatically compiles your source code to compile code also called as byte code, before executing it.Whenever we import a module for the first time or when your source file is a new file or we have an updated file then the recently compiled file, a .pyc file will be created on compiling the file in the same directory as the .py file (from python 3- you might see .pyc file is in a subdirectory called __pycache__ instead in the same directory as your .py file). This is a time-saver mechanism as it prevents python to skip the ... Read More

Python program to check if a number is Prime or not

AmitDiwan
Updated on 22-Aug-2023 00:23:58

196K+ Views

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number Let’s say the following is our input − 7 The output should be as follows − Prime Number Check if a number is Prime or not Let us check if a number if a Prime number or not using the for loop − Example # Number to be checked for prime n ... Read More

try and except in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

766 Views

To use exception handling in python, we first need to catch the all except clauses.Python provides, “try” and “except” keywords to catch exceptions. The “try” block code will be executed statement by statement. However, if an exception occurs, the remaining “try” code will not be executed and the except clause will be executed.try:    some_statements_here except:    exception_handlingLet’s see above syntax with a very simple example − Live Demotry:    print("Hello, World!") except:    print("This is an error message!")OutputHello, World!Above is a very simple example, let’s understand the above concept with another example − Live Demoimport sys List = ['abc', 0, 2, ... Read More

Defining Clean Up Actions in Python

Samual Sam
Updated on 30-Jun-2020 09:14:31

801 Views

There are numerous situation occurs when we want our program to do this specific task, irrespective of whether it runs perfectly or thrown some error. Mostly to catch at any errors or exceptions, we use to try and except block.The “try” statement provides very useful optional clause which is meant for defining ‘clean-up actions’ that must be executed under any circumstances. For example −>>> try:    raise SyntaxError finally:    print("Learning Python!") Learning Python! Traceback (most recent call last):    File "", line 2, in       raise SyntaxError    File "", line None SyntaxError: The final clause ... Read More

Writing files in background in Python

Samual Sam
Updated on 30-Jul-2019 22:30:24

140 Views

Here we are trying to do two tasks at a time, one in the foreground and the other in the background. We’ll write something in the file in the background and of a user input number, will find if it’s an odd or even number.Doing multiple tasks in one program in python is possible through multithreading in Live Demoimport threading import time class AsyncWrite(threading.Thread):    def __init__(self, text, out):       threading.Thread.__init__(self)       self.text = text       self.out = out    def run(self):       f = open(self.out, "a")       f.write(self.text + '') ... Read More

Difference between Method and Function in Python

karthikeya Boyini
Updated on 26-Aug-2023 08:56:37

28K+ Views

FunctionA function is a block of code to carry out a specific task, will contain its own scope and is called by name. All functions may contain zero(no) arguments or more than one arguments. On exit, a function can or can not return one or more values.Basic function syntaxdef functionName( arg1, arg2, ...):    ...    # Function_body    ...Let's create our own (user), a very simple function called sum (user can give any name he wants). Function "sum" is having two arguments called num1 and num2 and will return the sum of the arguments passed to the function(sum). When ... Read More

Python program to print duplicates from a list of integers?

AmitDiwan
Updated on 11-Aug-2022 11:41:43

2K+ Views

We will display duplicates from a list of integers in this article. The list is can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type Let’s say we have the following input list − [5, 10, 15, 10, 20, 25, 30, 20, 40] The output display duplicate elements − [10, 20] Print duplicates from a list of integers using for loop We will display the duplicates of a list of integer using a for loop. We ... Read More

Python program to print all the numbers divisible by 3 and 5 for a given number

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

18K+ Views

This is a python program to print all the numbers which are divisible by 3 and 5 from a given interger N. There are numerous ways we can write this program except that we need to check if the number is fully divisble by both 3 and 5.Below is my code to write a python program to print all the numbers divisible by 3 and 5 −lower = int(input("Enter lower range limit:")) upper = int(input("Enter upper range limit:")) for i in range(lower, upper+1):    if((i%3==0) & (i%5==0)):       print(i)OutputEnter lower range limit:0 Enter upper range limit:99 0 15 ... Read More

Difference between Python iterable and iterator

AmitDiwan
Updated on 12-Aug-2022 12:48:42

554 Views

What is an iterable? An iterable can be loosely defined as an object which would generate an iterator when passed to inbuilt method iter(). There are a couple of conditions, for an object to be iterable, the object of the class needs to define two instance method: __len__ and __getitem__. An object which fulfills these conditions when passed to method iter() would generate an iterator. Iterators Iterators are defined as an object that counts iteration via an internal state variable. The variable, in this case, is NOT set to zero when the iteration crosses the last item, instead, StopIteration() is ... Read More

Implementation of Dynamic Array in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

12K+ Views

Dynamic ArrayIn python, a list, set and dictionary are mutable objects. While number, string, and tuple are immutable objects. Mutable objects mean that we add/delete items from the list, set or dictionary however, that is not true in case of immutable objects like tuple or strings.In python, a list is a dynamic array. Let's try to create a dynamic list −>>> #Create an empty list, named list1 >>> list1 = [] >>> type (list1) Add some items onto our empty list, list1 −>>> # Add items >>> list1 =[2, 4, 6] >>> list1 [2, 4, 6] >>> # Another way ... Read More

Advertisements