-->

Python Essentials - FAQs and answers

 Learn Python with Questions and Answers

Python Essentials

In this post, we will try as much as we can to update Python in the shape of  FAQs - Frequently Asked Questions. This paper cannot be used to teach you python, but it will help you in 3 ways:

  • - Clarify some questions or matters of concern that may arise in your head
  • - Help you solve and pass some exams.
  • - Help you pass Python interviews.

Let's starts.

Why was the language called as Python?

Python got its name from a BBC comedy series from the seventies Monty Python's Flying Circus . The designer - Guido van Rossum - needed a name that was short, unique, and slightly mysterious. Since he was a fan of the show, he thought this name was great, so he decided to call the language "Python".

What is an interpreter?

In programming, an interpreter is the program that reads and executes code, and translates it into instructions for your computer to follow

What are literals in python? and what are their types?

A literal is data whose values are determined by the literal itself. Literals - or call them "the data in itself, or raw values, " - are the "output" or "values" that you get in the console. 123 is a literal, while c is not. You can guess the value of 123, but what is c? You never know. You use literals to encode data and to put them into your code.

Python has different types of literals:

  • String literals. "Hello" , "123"
  • Numeric literals. (Integers like: 1 , 2 , 100, Floats like: 1.5 , 2.0 , 100.00, Complex literals like:12j, Long literals. 89675L)
  • Boolean literals. True or False
  • Complex literals. 12j
  • Literal Collections. lists[] mutable/liable to change, tuples() immutable/unchanging, dictionaries{}mutable, set{}
  • Special literals. None

What is a sequence type?

A sequence type is a type of data in Python which is able to store more than one value (or less than one, as a sequence may be empty), and these values can be sequentially (hence the name) browsed, element by element.

- a sequence is data which can be scanned by the for loop.

- The list and dictionary are a classic example of a Python sequence.

What is mutability?

A property of any of Python's data that describes its readiness to be freely changed during program execution. There are two kinds of Python data: mutable and immutable.

Mutable data can be freely updated at any time. Ex. list.append[1], while Immutable data cannot be modified in this way. A tuple is an immutable sequence type.

What are python's membership and identity operators?

Membership Operators: 

(in , not in). Evaluates to true if it finds a variable in or not in the specified sequence and false otherwise. That's why it's called a "membership operator".

if ( x not in list ):
    print("x is NOT present in given list")
else:
    print("x is present in given list")

if ( y in list ):
    print("y is present in given list")
else:
    print("y is NOT present in given list")

Output:

x is NOT present in given list

y is present in given list

Identity Operators:

(is , is not). Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

x = 5.2
if (type(xis not int):
    print("true")
else:
    print("false")

Output:
true

Functions vs. methods. What is the difference ?

Before we know the differences, let know the definition and how they work.

A function is a "block of code" to carry out a specific task, will contain its own scope and is called by name. A function "doesn't belong to any data" - it gets data, it may create new data, and it (generally) produces a result. The function takes an argument, does something, and returns a result.

There are three kinds of functions:

1- Built-in functions:

come with the Python language, for example, help() to ask for any help, max()- to get maximum value, type()- to return the type of an object and many more.)

2- User-defined functions:

Functions that users create/define to help them. It is defined using def.

3- pre-installed modules:

They are  installed together with Python; the use of these functions requires some additional steps from the programmer in order to make them fully accessible

4- Anonymous Functions

Also called lambda functions. Unlike normal function which is defined using "def" keyword are defined using "lambda" keyword.

A method in python is somewhat similar to a function, except it is associated with object/classes. A method is a specific kind of function - it behaves like a function and looks like a function, but differs in the way in which it acts, and in its invocation style.

A method comes at the end of the data after the dot (data.insert(location, value), list.append(value))

Major differences between a function and a method:

  • The method is implicitly used for an object for which it is called. Methods are called on an object.
  • The method is accessible to data that is contained within the class.
  • Unlike method which can alter the object’s state, python function doesn’t do this and normally operates on it.
  • A method is owned by the data it works for, while a function is owned by the whole code.

What's the difference between a script and a program?

Scripts are programs with a short development cycle that can be created and deployed rapidly. A script is a program that is short, simple, and can be written very quickly. Python is a scripting language.

What is automation?

Automation is the process of replacing a manual step with one that happens automatically.

What is a "slice notation"?

A slice is an element of Python syntax that allows you to make a brand new copy of a list, or parts of a list. It actually copies the list's contents, not the list's name. In short, slicing is a flexible tool to build new lists out of an existing list. 

Since indexing allows you to access/change/delete only a single cell of a list, slice notation helps you get a sub-list of the list, update a bunch of cells at once, Or extend a list with an arbitrary number of new cells in any position.

- With slices we can extract an arbitrary part of a list, e.g.:

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]

nums[0:4] # We started from first (index 0) and stoped in 4.

[10, 20, 30, 40]

- We can use negative indexing:

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]

nums[-3:] # The "stop" is skipped, which means we take from start position(-3 = 70) till the "end"

[70, 80, 90]

- The step parameter:

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]

nums[::2] # Only every 2-nd element of the list

[10, 30, 50, 70, 90]

What is "recursion" ?

Recursion is a technique where a function invokes itself. When a function calls itself, this situation is known as recursion. You also need to know that recursive calls consume a lot of memory, and therefore may sometimes be inefficient.

Define Tuples ?

Tuples are ordered and unchangeable (immutable) collections of data. They can be thought of as immutable lists. They are written in round brackets ( ). Each tuple element may be of a different type (i.e., integers, strings, Booleans, etc.). Tuples can contain other tuples or lists (and the other way round). A one-element tuple may be created as follows:

one_element_tuple = ("one, )

one_element_tuple = "one, 

If comma is removed, python will create a variable, not a tuple.

Tuples are immutable, which means you cannot change their elements (you cannot append tuples, or modify, or remove tuple elements)

What is a debugger?

A debugger is a specialized piece of software that can control how your program is executed. Using the debugger, you can execute your code line-by-line, inspect all the variables' states and change their values on demand without modifying the source code, stop program execution when certain conditions are or aren't met, and do lots of other useful tasks. Every IDE is equipped with a more or less advanced debugger.

What is ASCII?

American Standard Code for Information Interchange (ASCII)
American Standard Code for Information Interchange (ASCII)

ASCII (short for American Standard Code for Information Interchange) is a universal and widely accepted standard character encoding that can be implemented by (almost) all computers, printers, mobile phones, tablets, etc. and operating systems all over the world. The code provides space for 256 different characters.

What is meant by BOM?

BOM (byte order mark) is an unprintable special combination of bits announcing encoding used by a file's content (eg. UCS-4 or UTF-B). Some utilities may require it.

Is Python 3 I18Ned?

Yes, it's completely internationalized - we can use UNICODE characters inside our code, read them from input and send to output.

What is overloading in python?

Overloading is the ability to use the same operator against completely different kinds of data (like numbers vs. strings) - as such an operator is overloaded with different duties.

What is meant by "Syntax" vs "Semantics"?

Syntax: The rules for how each instruction is written.
Semantics: The effects the instructions have..





👀 Keep following, we are adding more FAQs 👀

👀 Add your queries in the comments or ask and we are glad to help 👀


Post a Comment

0 Comments