What are local variables and global variables in Python?

Entry
  • Global Variables: Variables declared outside a function or in a global space are called global variables. These variables can be accessed by any function in the program.

  • Local Variables: Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.

When to use a tuple vs list vs dictionary in Python?

Entry
  • Use a tuple to store a sequence of items that will not change.

  • Use a list to store a sequence of items that may change.

  • Use a dictionary when you want to associate pairs of two items.

Explain some benefits of Python

Entry
  • Python is a dynamic-typed language. It means that you don’t need to mention the data type of variables during their declaration.

  • Python supports object-orientated programming as you can define classes along with the composition and inheritance.

  • Functions in Python are like first-class objects. It suggests you can assign them to variables, return from other methods and pass them as arguments.

  • Developing using Python is quick but running it is often slower than compiled languages.

  • Python has several usages like web-based applications, test automation, data modeling, big data analytics, and much more.

What is Lambda Functions in Python?

Junior

Lambda Function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

Consider:

x = lambda a : a + 10
print(x(5)) # Output: 15

 

How do I modify a string in python?

Junior

You can’t because strings are immutable in python. In most situations, you should simply construct a new string from the various parts you want to assemble it from. Work with them as lists; turn them into strings only when needed.

>>> s = list("Hello zorld")
>>> s
['H', 'e', 'l', 'l', 'o', ' ', 'z', 'o', 'r', 'l', 'd']
>>> s[6] = 'W'
>>> s
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
>>> "".join(s)
'Hello World'

 

What is a Negative Index in Python?

Junior

Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.

How the string does get converted to a number?

Junior
  • To convert the string into a number the built-in functions are used like int() a constructor. It is a data type that is used like int (‘1’) == 1.

  • float() is also used to show the number in the format as float(‘1’) = 1.

  • The number by default are interpreted as a decimal and if it is represented by int(‘0x1’) then it gives an error as ValueError. In this the int(string,base) the function takes the parameter to convert string to number in this the process will be like int(‘0x1’,16) == 16. If the base parameter is defined as 0 then it is indicated by octal and 0x indicates it as a hexadecimal number.

  • There is function eval() that can be used to convert a string into number but it is a bit slower and present many security risks

Login to Unlock Full Access

Create ProTechStack account To Unlock 1000+ Expert Questions, Jobs & Much More