I recently used eval() and exec() without even knowing how both the functions work and difference between them. Upon further investigation and learning I have found the main difference between eval() and exec(), in this short article I will be explaining those differences briefly.
Let’s start with simple definition of eval() and exec(),
eval(): It evaluate a string which contains single expression and return the calculated value
exec(): It execute a string which contains one or more expression or statements. It always returns None.
Eval function evaluates the python code and returns the value, but Exec execute the…
In python, assignment operator doesn’t copy the object, instead it copy the reference of object and store in new variable, so any changes in one variable will get reflected in another variable.
a = [1,2,3,4]
b = a
a: [1, 2, 3, 4] b: [1, 2, 3, 4]
a: [1, 2, 3, 4, 5] b: [1, 2, 3, 4, 5]
Let’s understand it better with the help of below visualization:
when a.append(5) gets executed, value 5 will get appended to same object on which list…
In this article, we will write few lines of code in python to draw heart and write text within it. To draw a heart, we will be using one of the python library known as ‘Turtle’.
Following are the functions which is being used for drawing:
Being a Programmer, one of the most important task is to analyse the code and optimize it , so that code should use less memory and time. One may think, finding a part of code which is taking up more space in memory is tedious task. But wait, fortunately in Python, we have different libraries for performance analysis, example: cProfile, line-profiler, guppy, memory-profiler. In this article, we’ll discuss memory-profiler library for monitoring memory consumption.
Module memory-profiler returns line-by-line analysis of memory consumption for python programs.
pip install -U memory_profiler
To get the line-by-line analysis follow below two steps:
It is very easy to perform copy/paste operations of Clipboard using ctrl+c and ctrl+v , you may think that performing clipboard operations using programming language may be difficult, but we can do this very easily with few lines of code using python. Python have libraries which is only dedicated for clipboard operations. In this short article, we will see three such python libraries.
pyperclip have methods copy() and paste() to perform copy/paste operation. It is a cross-platform library, which means we can use this library on different OS. …
In Python programming language, everything is an object. Even literals are also an object. If everything is an object, then definitely every object will have their own set of methods. Depending upon the type of literals, each literals have their own set of methods. In this short article, we are focusing only on integer object.
Let’s first have look at different operations we can perform on interger:
List is a data structure in python which is similar to dynamic array of other programming language. List is very excellent and useful data structure, it provides various functions, e.g. insert, append, sort, remove, pop etc, which makes our life easier. We can also create complex data structure like priority queue, stack etc, easily with the help of list in python.
If there are many perks of python list, definitely there will be some drawbacks of it. In this short article, we will see some issues that we can face while using python list along with their solutions.
In python , we don’t declare and specify the type of variable, we directly assign the value to variable. Though we don’t specify the type of variables in python, we cannot perform mathematical operations between variables of type integer and variable of type string. Now question arises, how python distinguishes the types of variables?
In this short articles, I will try to answer two questions:
To understand the answer of first question, you need to first understand the Static Typed…
The most important part of any programming language is to give proper name to variables and functions as it increases the readability of code. In any programming language we cannot use space while defining the variable, for instance, we want to store the name of programming languages in variable, we cannot just give variable name as ‘Programming language’, because space is used for special purpose.
In this short article, you are going to learn:
This is the mostly used type…
As we know, everything in python is an object, even function we define is also an object in Python. As a programmer, we write so many functions and also do use functions written by others.
When we define the function, we know what is written in function and how exactly it works, but when we use functions of other module we don’t know what’s the source code of it.
In this short article, you will learn method to get source code of functions in python.
To get the source code of functions in python, use inspect module. Inspect module have…