python means

Top 100 Python Interview Questions and Answers

We have listed Top 100 Python interview questions that have been designed for Python programmers who are preparing interviews on Python interviews. Python is regarded as being a great hobbyist language, yet it is also an extremely powerful language. It has bindings for C/C++ and Java so it can be used to tie large projects together or for rapid prototyping.

Python is also considered a high-level language, meaning it takes care of a lot of the grunt work involved in programming. For example, Python has a built-in garbage collector so you, as a programmer, don’t really need to worry about memory management.  Here, we have added some basic and advanced Best Python Interview Questions that help you to crack interview on Python Programming.

Top Python Interview Questions and Answers

Are you sure # is called the pound character?

I call it the octothorpe and that is the only name that no country uses and that works in every country. Every country thinks its way to call this one character is both the most important way to do it and also the only way it’s done. To me this is simply arrogance and, really, y’all should just chill out and focus on more important things like learning to code.

If # is for comments, then how come # – *- coding: utf- 8 – *- works?

Python still ignores that as code, but it’s used as a kind of “hack” or workaround for problems with setting and detecting the format of a fi le. You also fi nd a similar kind of comment for editor settings.

Why does the # in print “Hi # there.” not get ignored?

The # in that code is inside a string, so it will be put into the string until the ending ” character is hit. These pound characters are just considered characters and aren’t considered comments. 

How do I comment out multiple lines?

Put a # in front of each one. 

I can’t fi gure out how to type a # character on my country’s keyboard?

Some countries use the Alt key and combinations of those to print characters foreign to their language. You’ll have to look online in a search engine to see how to type it. 

Why do I have to read code backward?

It’s a trick to make your brain not attach meaning to each part of the code, and doing that makes you process each piece exactly. This catches errors and is a handy error- checking technique.

Why is the % character a “modulus” and not a “percent”?

Mostly that’s just how the designers chose to use that symbol. In normal writing, you are correct to read it as a “percent.” In programming, this calculation is typically done with simple division and the / operator. The % modulus is a different operation that just happens to use the % symbol. 

How does % work?

Another way to say it is “X divided by Y with J remaining.” For example, “100 divided by 16 with 4 remaining.” The result of % is the J part, or the remaining part. 

What is the order of operations?

In the United States we use an acronym called PEMDAS, which stands for Parentheses Exponents Multiplication Division Addition Subtraction. That’s the order Python follows as well. 

Why does / (divide) round down?

It’s not really rounding down; it’s just dropping the fractional part after the decimal. Try doing 7.0 / 4.0 and compare it to 7 / 4 and you’ll see the difference. 

What is the difference between = (single- equal) and == (double- equal)?

The = (single- equal) assigns the value on the right to a variable on the left. The == (double- equal) tests if two things have the same value, 

Can we write x=100 instead of x = 100?

You can, but it’s bad form. You should add space around operators like this so that it’s easier to read.

How can I print without spaces between words in print?

You do it like this: print “Hey %s there.” % “you”. You will do more of this soon.

What do you mean by “read the fi le backward”?

Very simple. Imagine you have a fi le with 16 lines of code in it. Start at line 16, and compare it to my fi le at line 16. Then do it again for 15, and so on, until you’ve read the whole fi le backward.

Why did you use 4.0 for space?

It is mostly so you can then fi nd out what a fl oating point number is and ask this question. See the Study Drills.

Can I make a variable like this: 1 = ‘Zed Shaw’?

No, the 1 is not a valid variable name. They need to start with a character, so a1 would work, but 1 will not.

What does %s, %r, and %d do again?

You’ll learn more about this as you continue, but they are “formatters.” They tell Python to take the variable on the right and put it in to replace the %s with its value. 

I don’t get it, what is a “formatter”? Huh?

The problem with teaching you programming is that to understand many of my descriptions, you need to know how to do programming already. The way I solve this is I make you do something, and then I explain it later. When you run into these kinds of questions, write them down and see if I explain it later. 

How can I round a fl oating point number?

You can use the round() function like this: round(1.7333). 

I get this error TypeError: ‘str’ object is not callable.

You probably forgot the % between the string and the list of variables.

Why does this not make sense to me?

Try making the numbers in this script your measurements. It’s weird, but talking about yourself will make it seem more real.

What is the difference between %r and %s?

We use %r for debugging, since it displays the “raw” data of the variable, but we use %s and others for displaying to users. 

What’s the point of %s and %d when you can just use %r?

The %r is best for debugging, and the other formats are for actually displaying variables to users.

If you thought the joke was funny could you write hilarious = True?

Yes, and you’ll learn more about these boolean values in Exercise 27. 

Why do you put ‘ (single- quotes) around some strings and not others?

Mostly it’s because of style, but I’ll use a single- quote inside a string that has double- quotes. Look at line 10 to see how I’m doing that. 

I get the error TypeError: not all arguments converted during string formatting.

You need to make sure that the line of code is exactly the same. What happens in this error is you have more % format characters in the string than variables to put in them. Go back and fi gure out what you did wrong.

How does the “end” statement work?

These are not really an “end statement,” but actually the names of variables that just happen to have the word “end” in them. 

Why are you using the variable named ‘snow’?

That’s actually not a variable: it is just a string with the word snow in it. A variable wouldn’t have the single- quotes around it. 

Is it normal to write an English comment for every line of code like you say to do in Study Drills #1?

No, normally you write comments only to explain diffi cult to understand code or why you did something. Why (or your motivation) is usually much more important, and then you try to write the code so that it explains how something is being done on its own. However, sometimes you just have to write such nasty code to solve a problem that it does need a comment on every line. In this case, though, it’s strictly for you to get better at translating from code to English. 

Can I use single- quotes or double- quotes to make a string or do they do different things?

In Python either way to make a string is acceptable, although typically you’ll use single- quotes for any short strings like ‘a’ or ‘snow’. 

Couldn’t you just not use the comma , and turn the last two lines into one single- line print?

Yes, you could very easily, but then it’d be longer than 80 characters, which in Python is bad style.

Why do I have to put quotes around “one” but not around True or False?

That’s because Python recognizes True and False as keywords representing the concept of true and false. If you put quotes around them, then they are turned into strings and won’t work right.  

I tried putting Chinese (or some other non- ASCII characters) into these strings, but %r prints out weird symbols.

Use %s to print that instead and it’ll work. 

Why does %r sometimes print things with single- quotes when I wrote them with double- quotes?

Python is going to print the strings in the most efficient way it can, not replicate exactly the way you wrote them. This is perfectly fine since %r is used for debugging and inspection, so it’s not necessary that it be pretty.

Why doesn’t this work in Python 3?

Don’t use Python 3. Use Python 2.7 or better, although Python 2.6 might work fine.

Can I use IDLE to run this?

No, you should learn to use the command line. It is essential to learning programming and is a good place to start if you want to learn about programming. 

What if I wanted to start the months on a new line?

You simply start the string with \n like this:

“\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”

Why do the \n newlines not work when I use %r?

That’s how %r formatting works; it prints it the way you wrote it (or close to it). It’s the “raw” format for debugging.

Why do I get an error when I put spaces between the three double- quotes?

You have to type them like “”” and not ” ” “, meaning with no spaces between each one. 

Is it bad that my errors are always spelling mistakes?

Most programming errors in the beginning (and even later) are simple spelling mistakes, typos, or getting simple things out of order. 

I still haven’t completely figured out the last exercise. Should I continue?

Yes, keep going, and instead of stopping, take notes listing things you don’t understand for each exercise. Periodically go through your notes and see if you can figure these things out after you’ve completed more exercises. Sometimes, though, you may need to go back a few exercises and go through them again.

What makes \\ special compared to the other ones?

It’s simply the way you would write out one backslash (\) character. Think about why you would need this. 

When I write // or /n it doesn’t work.

That’s because you are using a forward- slash / and not a backslash \. They are different characters that do very different things. 

When I use a %r format none of the escape sequences work.

That’s because %r is printing out the raw representation of what you typed, which is going to include the original escape sequences. Use %s instead. Always remember this: %r is for debugging; %s is for displaying.

How do I get a number from someone so I can do math?

That’s a little advanced, but try x = int(raw_input()), which gets the number as a string from raw_input() then converts it to an integer using int(). 

I put my height into raw input like raw_input(“6’2”) but it doesn’t work.

You don’t put your height in there; you type it directly into your Terminal. First thing is, go back and make the code exactly like mine. Next, run the script, and when it pauses, type your height in at your keyboard. That’s all there is to it. 

Why do you have a new line on line 8 instead of putting it on one line?

That’s so that the line is less than 80 characters long, which is a style that Python programmers like. You could put it on one line if you like. 

What’s the difference between input() and raw_input()?

The input() function will try to convert things you enter as if they were Python code, but it has security problems so you should avoid it. 

When my strings print out there’s a u in front of them, as in u’35’.

That’s how Python tells you that the string is Unicode. Use a %s format instead and you’ll see it printed like normal. 

How come I get SyntaxError: invalid syntax whenever I run pydoc?

You aren’t running pydoc from the command line; you’re probably running it from inside python. Exit out of python first. 

Why does my pydoc not pause like yours does?

Sometimes if the help document is short enough to fi t on one screen, then pydoc will just print it.

When I run pydoc I get more is not recognized as an internal.

Some versions of Windows do not have that command, which means pydoc is broken for you. You can skip this Study Drill and just search online for Python documentation when you need it.

Why would I use %r over %s?

Remember, %r is for debugging and is “raw representation” while %s is for display. I will not answer this question again, so you must memorize this fact. This is the #1 thing people ask repeatedly, and asking the same question over and over means you aren’t taking the time to memorize what you should. Stop now, and fi nally memorize this fact.

Why can’t I do print “How old are you?” , raw_input()?

You’d think that’d work, but Python doesn’t recognize that as valid. The only answer I can really give is, you just can’t. 

When I run it I get ValueError: need more than 1 value to unpack.

Remember that an important skill is paying attention to details. If you look at the What You Should See (WYSS) section, you see that I run the script with parameters on the command line. You should replicate how I ran it exactly. 

What’s the difference between argv and raw_input()?

The difference has to do with where the user is required to give input. If they give your script inputs on the command line, then you use argv. If you want them to input using the keyboard while the script is running, then use raw_input(). 

Are the command line arguments strings?

Yes, they come in as strings, even if you typed numbers on the command line. Use int() to convert them just like with raw_input(). 

How do you use the command line?

You should have learned to use it real quick by now, but if you need to learn it at this stage, then read the Command Line Crash Course appendix.

I can’t combine argv with raw_input().

Don’t over think it. Just slap two lines at the end of this script that uses raw_input() to get something and then print it. From that, start playing with more ways to use both in the same script.

Why can’t I do this raw_input(‘? ‘) = x?


Because that’s backward. Do it the way I do it and it’ll work. 

I get SyntaxError: invalid syntax when I run this script.

Again, you have to run it right on the command line, not inside Python. If you type python and then try to type python ex14.py Zed, it will fail because you are running Python inside Python. Close your window and then just type python ex14.py Zed. 

I don’t understand what you mean by changing the prompt?

See the variable prompt = ‘> ‘. Change that to have a different value. You know this; it’s just a string and you’ve done 13 exercises making them, so take the time to fi gure it out.

I get the error ValueError: need more than 1 value to unpack.

Remember when I said you need to look at the WYSS section and replicate what I did? You need to do the same thing here and focus on how I type the command in and why I have a command line argument.

Can I use double- quotes for the prompt variable?

You totally can. Go ahead and try that. 

You have a Tandy computer?

I did when I was little. 

Does txt = open(filename) return the contents of the fi le?

No, it doesn’t. It actually makes something called a “fi le object.” You can think of it like an old tape drive that you saw on mainframe computers in the 1950s or even like a DVD player from today. You can move around inside them, and then “read” them, but the fi le is not the contents. 

I can’t type code into my Terminal/PowerShell like you say in Study Drill #7.

First thing, from the command line just type python and hit Enter. Now you are in python as

we’ve done a few other times. Once you have that you can just type in code and Python will run it in little pieces. Play with that. To get out of it type quit() and hit Enter. 

What does from sys import argv mean?

For now, just understand that sys is a package, and this phrase just says to get the argv feature from that package. You’ll learn more about these later. 

I put the name of the fi le in as script, ex15_sample.txt = argv but it doesn’t work.

No, that’s not how you do it. Make the code exactly like mine, then run it from the command line the exact same way I do. You don’t put the names of files in; you let Python put the name in.

Why is there no error when we open the file twice?

Python will not restrict you from opening a file more than once, and in fact sometimes this is necessary.

Is the truncate() necessary with the ‘w’ parameter?

See Study Drills #5.

What does ‘w’ mean?

It’s really just a string with a character in it for the kind of mode for the fi le. If you use ‘w’, then you’re saying “open this fi le in ‘write’ mode”—hence the ‘w’ character. There’s also ‘r’ for “read,” ‘a’ for append, and modifiers on these.

What are the modifi ers to the fi le modes we can use?

The most important one to know for now is the + modifi er, so you can do ‘w+’, ‘r+’, and ‘a+’. This will open the fi le in both read and write mode and, depending on the character used, position the fi le in different ways. 

Does just doing open(filename) open it in ‘r’ (read) mode?

Yes, that’s the default for the open() function. 

Why is the ‘w’ in quotes?

That’s a string. You’ve been using them for a while now, so make sure you know what a string is. 

No way you can make this one line!

That ; depends ; on ; how ; you ; defi ne ; one ; line ; of ; code. 

What does the len() function do?

It gets the length of the string that you pass to it and then returns that as a number. Play with it. 

When I try to make this script shorter, I get an error when I close the fi les at the end.

You probably did something like this, indata = open(from_file).read(), which means you don’t need to then do in_file.close() when you reach the end of the script. It should already be closed by Python once that one line runs.

Is it normal to feel like this exercise was really hard?

Yes, it is totally normal. Programming may not “click” for you until maybe even Exercise 36, or it might not until you fi nish the book and then make something with Python. Everyone is different, so just keep going and keep reviewing exercises that you had trouble with until it clicks. Be patient. 

I get a Syntax:EOL while scanning string literal error.

You forgot to end a string properly with a quote. Go look at that line again.

What’s allowed for a function name?

Just like variable names, anything that doesn’t start with a number and is letters, numbers, and underscores will work.

What does the * in *args do?

That tells Python to take all the arguments to the function and then put them in args as a list. It’s like argv that you’ve been using, but for functions. It’s not normally used too often unless specifi -cally needed. 

This feels really boring and monotonous.

That’s good. It means you’re starting to get better at typing in the code and understanding what it does. To make it less boring, take everything I tell you to type in, and then break it on purpose. 

Is there a way to analyze what this function is doing so I can understand it better?

There’s many different ways, but try putting an English comment above each line describing what the line does. Another trick is to read the code out loud. Yet another is to print the code out and draw on the paper with pictures and comments showing what’s going on. 

What if I want to ask the user for the numbers of cheese and crackers?

Remember, you just need to use int() to convert what you get from raw_input(). 

Does making the variables on lines 13 and 14 change the variables in the function?

Nope, those variables are separate and live outside the function. They are then passed to the function and temporary versions are made just for the function’s run. When the function exits, these temporary variables go away and everything keeps working. Keep going in the book and this should become clearer.

Online Training Tutorials

  • How to Install Python in Windows?How to Install Python in Windows?Installing Python Python installation in Windows : To install Python, you must first download the installation package of your preferred version from this link: You will be given the […]
  • Python Files,Test Code and ScriptsPython Files,Test Code and ScriptsPython Files: Files, which are also known as modules, are where we can store our code. This is really helpful for more serious programming, because we don't want to start over on a lengthy […]
  • Python functionsPython functionsWe are able to create our own Python functions. Use the interpreter, generally, if you only need to do a calculation once. However, when you or others need to make a specific type of […]
  • What is Python? Python IntroductionWhat is Python? Python IntroductionPython is the name of a powerful modern computer programming language. It resembles Fortran, one of the first programming languages, to some extent, but Fortran is far less powerful than […]
  • SAP system developmentTo Understand essential aspects of SAP System developmentThis article describes the essential aspects of SAP system development. The resources and materials will be abundant like a sea, but the sole purpose of this writing is to give bullet […]
  • Vendor Consignment StockWhat is SAP Procurement – Vendor Consignment Stock?Vendor Consignment Stock This is a special kind of stock which can be viewed under SAP inventory transaction MMBE but it belongs to the vendor. As per the need, the company withdraws […]
  • Master DataHow to delete Master Data in SAP?Steps to Follow for Master Data in SAP Please Follow the below steps to delete the master data. 1) RSA1 --> find your Info object --> right click --> maintain master […]
  • Importing projects from SAP Solution ManagerSAP Solution Manager : We have to make the necessary system settings for the Scheduling a Job scenario in the SAP Solution Manager customizing, under  Configuration Scenario-Specific […]