Python Flashcards

1
Q

Comments In python?

hint: caveat?

A

they begin with #
This statement is ignored by the interpreter and serves as documentation for our code.

is used for single-line comments in Python

””” this is a comment “”” is used for multi-line comments, not ignored by the interpreter, these are called docstrings.

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

Imperative programming:

A

In computer science, imperative programming is a programming paradigm of software that uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform

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

Python features:

6

A
  1. Dynamically types: 1.No need to declare anything. An assignment statement binds a name to an object, and the object can be of any type.2.No type casting is required when using container objects
  2. Interpreted: bytecode then into machine lang.
  3. Uses indentation to structure code.
  4. Platform Independent.
  5. High-level lang: No need to take care of memory used by program, etc.
  6. Embeddable. It can be used within C/C++.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Python 2 VS Python 3

A
  1. / in 2 gives int and 3 gives float.
  2. print in 2 is keyword and 3 is function.
    print ‘Py 2’ and print(‘python 3’)
    …….
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

input()

how to get int iutput?
how to ask the user to give certain input?

A

input ( ):
This function first takes the input from the user and converts it into a string.
The type of the returned object always will be string.
It does not evaluate the expression it just returns the complete statement as String.

var = input(“Text to display on output screen”)

to get input:
var = int(input()) # typecast

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

What is Console in Python?

A

Console (also called Shell) is basically a command-line interpreter that takes input from the user i.e one command at a time and interprets it.

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

how do we take multiple inputs from the user?

A
Using split() method
Using List comprehension: yet to explore
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

split()

A

x,y = input().split(separator, maxsplit)

  • default separator is white space.
  • it breaks the string by the specified separator.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

print()

attributes? (5)

A

Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)

Parameters:

value(s): Any value, and as many as you like. Will be converted to a string before printed
sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default:’ ‘
end=’end’: (Optional) Specify what to print at the end.Default: ‘\n’
file : (Optional) An object with a write method. Default:sys.stdout
flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False
Returns: It returns output to the screen.

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

flush argument of print:

A

If false, it is buffered. if true it is flushed.
The I/Os in python are generally buffered, meaning they are used in chunks.
If it is set to true, the output will be written as a sequence of characters one after the other. This process is slow simply because it is easier to write in chunks rather than writing one character at a time.

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

Print a list without newline:

A
# using * symbol prints the list
# elements in a single line
print(*list_name)

Example:

>>> list = [10,20,30,40]
>>> print(list)
[10, 20, 30, 40]
>>> print(*list)
10 20 30 40
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Character in python?

A

No such data type in python.

It is simply a string with length 1.

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

String in python?

how to create them?

how to access them?
indexing type here?

A
  • an array of bytes representing Unicode characters.
  • single, double or triple quotes to create them.
  • triple quotes allow multi-lines.
  • access using indexing, starts from 0.
  • negative indexing is also there: -1 refers to the last character.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

String slicing?

A
  • to access a range of characters.
  • colon : is the slicing operator.
  • string[3:12] //12th not included. 3rd included.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Updation and deletion

string

A

Strings are immutable. a character can’t be changed or deleted.
the entire string can be deleted.

> > > del string_name

and we can reassign a new string to the same one.

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

Escape Sequencing in python:

A
  • triple quotes allow the use of double and single strings.
  • the backlash is used to escape.

' will print ‘
\ will print “

17
Q

format():

A

Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.

# Python Program for
# Formatting of Strings
# Default order
String1 = "{} {} {}".format('Geeks', 'For', 'Life')
print("Print String in default order: ")
print(String1)
# Positional Formatting
String1 = "{1} {0} {2}".format('Geeks', 'For', 'Life')
print("\nPrint String in Positional order: ")
print(String1)
# Keyword Formatting
String1 = "{l} {f} {g}".format(g='Geeks', f='For', l='Life')
print("\nPrint String in order of Keywords: ")
print(String1)
18
Q

justify a string?

or operator %

A
  • use of for left, center, right justification respectively.

A string can be left(

19
Q

Size of list?

A

len(List)

20
Q

How to add elements to the desired position in the list?

A

list.insert(position,val)

list also allows negative indexing.

21
Q

Add more than one element at the end of the list

A

list.extend([val1,val2,val3]}

22
Q

delete an element from list:

2 ways:

A
  1. list.remove(element_value)
    removes only one element, returns error if element not in list.
  2. list.pop() removes and returns last element.
    list. pop(index_position) removes and returns element at that position.
23
Q

Slicing of the list:

A

to print in reverse order: list[::-1]
in range: [start index: end index]
whole list: list[:]

to print from last:
[:-6] use negative index then beginning becomes last.

24
Q

List comprehension:

A

List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc.

A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

newList = [ expression(element) for element in oldList if condition ]

25
Q

Sort list in ascending and descending order

A

list.sort() #ascending

list_name.sort(reverse=True)
This will sort the given list in descending order.

26
Q

Create a tuple:

3

A
  • by placing a sequence of values separated by ‘comma’ with or without the use of parentheses for grouping the data sequence
  • tuple(list1) #typecasting
  • tuple(‘GEEKS’)

makes a tuple like: (‘G’,’E’,’E’,’K’,’S’)

27
Q

Unpacking a tuple:

A
# Program to understand about
# packing and unpacking in Python
# this lines PACKS values
# into variable a
a = ("MNNIT Allahabad", 5000, "Engineering")
# this lines UNPACKS values
# of variable a
(college, student, type_ofcollege) = a

now college = MNNIT Allahabad

28
Q

Concatenation of tuples:

A

nt = t1 + t2

29
Q

Shorthand if else

A

statement_when_True if condition else statement_when_False

# Python program to illustrate short hand if-else
i = 10
print(True) if i < 15 else print(False)
30
Q

if else syntax in python

A
if (condition):
    statement
elif (condition):
    statement
.
.
else:
    statement