Basic python Flashcards

1
Q

What is Python?

A

Python is a high-level, interpreted, general-purpose programming language. Python supports objects, modules, threads, exception-handling, and automatic memory management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the benefits of using Python?

A

Readable, open source, and supports third-party packages encouraging modularity and code reuse.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a dynamically typed language?

A

In a strongly-typed language, such as Python, “1” + 2 will result in a type error since these languages don’t allow for “type-coercion” (implicit conversion of data types). On the other hand, a weakly-typed language, such as Javascript, will simply output “12” as result.

Type-checking can be done at two stages:

Static - Data Types are checked before execution.
Dynamic - Data Types are checked during execution.

Python is an interpreted language, executes each statement line by line and thus type-checking is done on the fly, during execution. Hence, Python is a Dynamically Typed Language.

Statically typed languages perform type checking at compile-time, while dynamically-typed languages perform type checking at run-time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an Interpreted language?

A

An Interpreted language executes its statements line by line. Languages such as Python, Javascript, R, PHP, and Ruby are prime examples of Interpreted languages. Programs written in an interpreted language runs directly from the source code, with no intermediary compilation step.

Interpreted languages were once significantly slower than compiled languages. But, with the development of just-in-time compilation, that gap is shrinking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a compiled language?

A

Compiled languages are converted directly into machine code that the processor can execute. As a result, they tend to be faster and more efficient to execute than interpreted languages. They also give the developer more control over hardware aspects, like memory management and CPU usage.

Compiled languages need a “build” step – they need to be manually compiled first. You need to “rebuild” the program every time you need to make a change.

Examples of pure compiled languages are C, C++, Erlang, Haskell, Rust, and Go.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Scope in Python?

A

Every object in Python functions within a scope. A scope is a block of code where an object in Python remains relevant. Namespaces uniquely identify all the objects inside a program. However, these namespaces also have a scope defined for them where you could use their objects without any prefix. A few examples of scope created during code execution in Python are as follows:

  • A local scope refers to the local objects available in the current function.
  • A global scope refers to the objects available throughout the code execution since their inception.
  • A module-level scope refers to the global objects of the current module accessible in the program.
  • An outermost scope refers to all the built-in names callable in the program. The objects in this scope are searched last to find the name referenced
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are lists and tuples? What is the key difference between the two?

A

Lists and Tuples are both sequence data types that can store a collection of objects in Python. The objects stored in both sequences can have different data types. Lists are represented with square brackets, while tuples are represented with parentheses.

The key difference between the two is that while lists are mutable, tuples are immutable objects. So lists can be modified, appended or sliced on the go but tuples remain constant and cannot be modified in any manner.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is pass in Python?

A

The pass keyword represents a null operation in Python. It is generally used for the purpose of filling up empty blocks of code which may execute during runtime but has yet to be written.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are modules and packages in Python?

A

Python packages and Python modules are two mechanisms that allow for modular programming in Python. Modularizing has several advantages - simplicity, maintainability, reusability, and scoping.

Modules, are simply Python files and can have a set of functions, classes, or variables defined and implemented. They can be imported and initialized once using the import statement. If partial functionality is needed, import the requisite classes or functions using from foo import bar.

Packages allow for hierarchial structuring of the module namespace using dot notation. As, modules help avoid clashes between global variable names, in a similar manner, packages help avoid clashes between module names.

The package is a simple directory having collections of modules. This directory contains Python modules and also having __init__.py file by which the interpreter interprets it as a Package. The package is simply a namespace. The package also contains sub-packages inside it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are global, protected and private attributes in Python?

A

Global variables are public variables that are defined in the global scope. To use the variable in the global scope inside a function, we use the global keyword.

Protected attributes are attributes defined with an underscore prefixed to their identifier eg. _sara. They can still be accessed and modified from outside the class they are defined in but a responsible developer should refrain from doing so.

Private attributes are attributes with double underscore prefixed to their identifier eg. __ansh. They cannot be accessed or modified from the outside directly and will result in an AttributeError if such an attempt is made.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the use of self in Python?

A

Self is used to represent the current instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. self is used in different places and often thought to be a keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is __init__?

A

__init__ is a contructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a __init__ method associated with them. It helps in distinguishing methods and attributes of a class from local variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is break, continue and pass in Python?

A

Break: the break statement terminates the loop immediately and the control flows to the statement after the body of the loop.

Continue: the continue statement terminates the current iteration of the statement, skips the rest of the code in the current iteration and the control flows to the next iteration of the loop.

Pass: the pass keyword in Python is generally used to fill up empty blocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is slicing in Python?

A
  • As the name suggests, ‘slicing’ is taking parts of an iterable
  • Syntax for slicing is [start : stop : step]
  • Default value for start is 0, stop is number of items, step is 1.
  • Slicing can be done on strings, arrays, lists, and tuples.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain how can you make a Python Script executable on Unix?

A

Script file must begin with #!/usr/bin/env python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between Python Arrays and lists?

A

Arrays in python can only contain elements of same data types i.e., data type of array should be homogeneous. It is a thin wrapper around C language arrays and consumes far less memory than lists.

Lists in python can contain elements of different data types i.e., data type of lists can be heterogeneous. It has the disadvantage of consuming large memory.