top of page
Anchor 1
Frequently Asked C Interview Question & Answers

Is indentation required in python?

Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc is specified within an indented block. It is usually done using four space characters. If your code is not indented necessarily, it will not execute accurately and will throw errors as well.

Close

What do you mean by *args and **kwargs?

In cases when we don’t know how many arguments will be passed to a function, like when we want to pass a list or a tuple of values, we use *args.

>>> def func(*args):

for i in args:

print(i)

>>> func(3,2,1,4,7)

3
2
1
4
7


**kwargs takes keyword arguments when we don’t know how many there will be.

>>> def func(**kwargs):

for i in kwargs:

print(i,kwargs[i])

>>> func(a=1,b=2,c=7)

a.1
b.2
c.7

The words args and kwargs are a convention, and we can use anything in their place.

Close

Write Python logic to count the number of capital letters in a file.

>>> import os

>>> os.chdir('C:\\Users\\lifei\\Desktop')

>>> with open('Today.txt') as today:

count=0

for i in today.read():

if i.isupper():

count+=1

print(count)

26

Close

What are negative indices?

Let’s take a list for this.

>>> mylist=[0,1,2,3,4,5,6,7,8]

 

A negative index, unlike a positive one, begins searching from the right.

>>> mylist[-3]

6

This also helps with slicing from the back:

>>> mylist[-6:-1]

[3, 4, 5, 6, 7]

Close

How would you randomize the contents of a list in-place?

For this, we’ll import the function shuffle() from the module random.

>>> from random import shuffle

>>> shuffle(mylist)

>>> mylist

[3, 4, 8, 0, 5, 7, 6, 2, 1]  

Close

Explain join() and split() in Python.

join() lets us join characters from a string together by a character we specify.

>>> ','.join('12345')

‘1,2,3,4,5’

 

split() lets us split a string around the character we specify.

>>> '1,2,3,4,5'.split(',')

[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]

Close

How long can an identifier be in Python?

In Python, an identifier can be of any length. Apart from that, there are certain rules we must follow to name one:

  1. It can only begin with an underscore or a character from A-Z or a-z.

  2. The rest of it can contain anything from the following: A-Z/a-z/_/0-9.

  3. Python is case-sensitive, as we discussed in the previous question.

  4. Keywords cannot be used as identifiers. Python has the following keywords:

and, def, False, import, not, True, as, del, finally, in, or, try, assert, elif, for, is, pass, while, break, else, from, lambda, print, with, class, except, global, None, raise, yield, continue, exec, if, nonlocal, return.

Close

How do you remove the leading whitespace in a string?

Leading whitespace in a string is the whitespace in a string before the first non-whitespace character. To remove it from a string, we use the method lstrip().

>>> ' Ayushi '.lstrip()

‘Ayushi   ' 

As you can see, this string had both leading and trailing whitespaces. lstrip() stripped the string of the leading whitespace. If we want to strip the trailing whitespace instead, we use rstrip().

>>> ' Ayushi '.rstrip()

‘   Ayushi’

Close

How would you convert a string into lowercase?

We use the lower() method for this.

>>> 'AyuShi'.lower()

‘ayushi’

To convert it into uppercase, then, we use upper().

>>> 'AyuShi'.upper()

‘AYUSHI’

Also, to check if a string is in all uppercase or all lowercase, we use the methods isupper() and islower().

>>> 'AyuShi'.isupper()

False

>>> 'AYUSHI'.isupper()

True

>>> 'ayushi'.islower()

True

>>> '@yu$hi'.islower()

True

>>> '@YU$HI'.isupper()

True

So, characters like @ and $ will suffice for both cases.

Also, istitle() will tell us if a string is in title case.

>>> 'The Corpse Bride'.istitle()

True

Close

What is the pass statement in Python?

There may be times in our code when we haven’t decided what to do yet, but we must type something for it to be syntactically correct. In such a case, we use the pass statement.

>>> def func(*args):

pass

>>>

Similarly, the break statement breaks out of a loop.

>>> for i in range(7):

if i==3: break

print(i)

1

2

Finally, the continue statement skips to the next iteration.

>>> for i in range(7):

if i==3: continue

print(i)

1
2
4
5
6

Close
bottom of page