Python lovers must know !!

Hello everyone, I am Shubham Agrawal. I am doing well hope you all are also fine. Due to the COVID-19 outbreaks you all are sitting at home and trying to learn something new. Well, I have also brought such kind of stuff and will try to share with you in less amount of time. Today I would like to share some cool and very essentials things which are based on Python programming language. As we all know that Python is very versatile language and so many students are sitting voracious to learn about it. This blog is not for those who haven’t put their leg in the python programming language yet because in this blog I will discuss some different things which all programmers who have the knowledge about python can easily understand and rely with me. If you want to learn any programming language you have to be patient and indulge in it. But sometimes lack of practice many students forget about it. Don’t worry friends I will discuss only those topics in which there may be a chance you can stuck. If you already know all those then you can revise it and if you have superficial knowledge about it then you can learn and practice all those. You can say this blog is as refresher to those who have knowledge about python programming language. I am not starting from scratch because there are lots of posts and video are available on Google, YouTube etc.  So don’t waste our time let’s gets started.

!!Python Programming!!
   
   

Note: - This blog is based on Python3 and I assuming that you have knowledge about python very well because I will not teach you. Only I will discuss some features provided by the python3.

 

1. Is python backward compatible?

Answer:- No, Python is backward incompatible language because If you know, in python2 print is a statement but from python3 onwards print is now a function.

2. What is the Caching range of data type in python3?

Answer:- Python caches the integer value in range between -5 to 256. Beyond this range python creates new object even for the same value. The concept caching and object reusability applies to Non- Space string and Booleans but not apply to float and complex data type.

3. How floor division work in python?

Answer:-


+ve= If both (divisor and dividend) are positive and integer then decimal part will be truncated.

-ve= If one of them is negative then answer will be negative and floored.

If both int then answers will be integer otherwise float.

4. Another role of operator *?

Answer:- As we all know that String in python can be truncated with String only otherwise it will give “TypeError” to keep in mind python has feature a to repeat String by “*” operator in python. For Example:-

            >>>str=”ba”+”na”*2

            output:- ‘banana’

5. Math module in python.

Answer:- Math module is very useful module in python. If you are a Competitive programming lover than It will definitely reduce your efforts while making program if required. If you want to use them then you have to import in your program.

Useful methods:-

math.factorial()

math.floor()

math.ceil()

math.gcd()

math.pow()

math.sqrt()

All the above methods are work accordingly as the name implies but I have seen most of the times students little bit confuse about math.floor() and math.ceil(). Don’t worry I will explain you in short..

1)math.floor():- Here, floor is the greatest integer less than n. What does it means? I will show with an example:-

suppose we have a float number n=3.5 then if we write math.floor(3.5) then output will be 3.

2)math.ceil():- here, Ceil is the smallest integer greater than n. if we have n=3.5 then math.ceil(3.5) will be 4.

 

Useful Constants:-

math.pi

math.e

math.tau:- It is 2*math.pi

6. What are the different way to write print() function in python.

Answer:-

There are mainly five way to write print() function in python which is generally programmer write while making program in python3

It can be better understand with an example:-

Suppose you want to print the value of A which is 25 then you can write print() function as:-

1)print("Value of A is "+str(A))

 

2)print("value of A is",A)

 

3)print("value of A is %d" %A)

 

4)print("value of A is {}".format(A))

 

5)print(f"value of A is {A}")

 

From above five my favorites is last one Because we can write any valid expression in “{ }” area.

8. Membership and identity operator in python.

Answer:- Both are very powerful operator in python there are lots of operator but amongst all these two are very popular. It’s importance can be understand by compare python with other language. Like in c/c++, Java, If we want to search any element from array or list then what we have to do is? We have to apply linear search or binary search on it which is very time consuming. If you want to do in one single word in python then Membership operator comes in a picture.

As the name implies, it search a element in the list as a member if present then return True otherwise False.

For example:-

    list=[10,20,30,40,50]

    x=50

    if x in list:

           print(“Yes”)

    else:

           print(“No”)

Similarly identity operator is used to check whether it is True or not. 

For example:-

Suppose we have an integer A=25 and we want to check whether it belongs to int class or not then all we have to do is.

    if type(A)==int:

         print(“Yes”)

    else:

         print(“No”)

9. How to get rid from NZEC error in python.

Answer:- Sometimes, When we do competitive programming and try to make a program correct but when we submit it sometimes we are getting NZEC error. NZEC stands for Non-Zero exit error. There are numerous reasons one of those is sometimes we forget to remove whitespace while taking an input etc. To get rid from it we have to perform exception handling

For example:-

try:

     //write your whole code here

 

except:

     pass

 

Note:- But the important things to be remember your code must be properly valid because if there some other error except NZEC will also be pass from except block so that you may get Wrong answer. So before making try..except make sure your code doesn’t have any other error.


I think it is more than enough for PART-1. Hope it is very helpful for you guys. I will definitely make another blog which is PART-2 where we will discuss some advance feature of python and very important slicing on String in Python3 as well.

 

Thank you for reading this blog. If you find something wrong and correction in which I have discuss above then feel free to write your queries, doubts, suggestion in the comment section all are always welcome . Stay safe and Stay healthy.


                       **Thank you**

Comments