Basics Flashcards

(36 cards)

1
Q

where does the name come from

A

BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles

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

possible interpreter installation locations

A

/usr/local/bin/python3.6
/usr/local/python
windows: C:\Python36

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

exit interpreter

A

ctrl+ D, ctrl+Z, quit()

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

what is sys.argv for?

A

the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module

import sys

print ‘Number of arguments:’, len(sys.argv), ‘arguments.’
print ‘Argument List:’, str(sys.argv)

$ python test.py arg1 arg2 arg3

Number of arguments: 4 arguments.
Argument List: [‘test.py’, ‘arg1’, ‘arg2’, ‘arg3’]

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

What does TTY stand for?

A

Early user terminals connected to computers were electromechanical teleprinters or teletypewriters (TeleTYpewriter, TTY), and since then TTY has continued to be used as the name for the text-only console.

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

What is the default encoding for Python source files?

A

UTF-8

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

To declare an encoding other than default one what should one do?

A
The first line of the file should be as follows:
# -*- coding: encoding -*-
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is there any exception to declaring encoding as first line?

A
Yes, when the source code starts with a UNIX shebang line.
#!/usr/bin/env python3
# -*- coding: cp-1252 -*-
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the comment for Python?.

A

A line starting with #. But # inside string literal is just a #, not a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
explain /
// %
A
/ classic division
// floor division
% returns remainder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

explain **

A

used for power

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

What happens when you operate a mixture of integer and floating points?

A

Operands convert the integer operand to floating point.

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

What _ is used for in interactive mode?

A

The last printed expression is assigned to the variable _. Its read only. Assigning a value creates a local variable masking it.

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

What number types are supported?

A

int, float, Decimal, Fraction, complex numbers.

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

> > > ‘spam eggs’

> > > ‘doesn't’

> > > “doesn’t”

> > > ‘“Yes,” he said.’

> > > ”"Yes," he said.”

> > > ‘“Isn't,” she said.’

A
>>> 'spam eggs'  # single quotes
'spam eggs'
>>> 'doesn\'t'  # use \' to escape the single quote...
"doesn't"
>>> "doesn't"  # ...or use double quotes instead
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

> > > ‘“Isn't,” she said.’

> > > print(‘“Isn't,” she said.’)

> > > s = ‘First line.\nSecond line.’

> > > s

> > > print(s)

A

> > > ‘“Isn't,” she said.’
‘“Isn't,” she said.’
print(‘“Isn't,” she said.’)
“Isn’t,” she said.
s = ‘First line.\nSecond line.’ # \n means newline
s # without print(), \n is included in the output
‘First line.\nSecond line.’
print(s) # with print(), \n produces a new line
First line.
Second line.

17
Q

> > > print(‘C:\some\name’)

> > > print(r’C:\some\name’)

A

> > > print(‘C:\some\name’) # here \n means newline!
C:\some
ame
print(r’C:\some\name’) # note the r before the quote
C:\some\name

18
Q

How do you span string literals multiple lines?

A

using either ‘'’…’’’ or “"”…”””

if you do not want newlines put \ after each line.

19
Q

print(“””\
Usage: thingy [OPTIONS]
-h Display this usage message\
-H hostname Hostname to connect to\
“””)

A

Usage: thingy [OPTIONS]

-h Display this usage message -H hostname Hostname to connect to

20
Q

print(“””\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
“””)

A

Usage: thingy [OPTIONS]

 - h                        Display this usage message
 - H hostname               Hostname to connect to
21
Q

3 * ‘un’ + ‘ium’

A

‘unununium’

22
Q

‘Py’ ‘thon’

A

‘Python’
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.Only works for literals.

23
Q

text = (‘Put several strings within parentheses ‘

‘to have them joined together.’)

A

‘Put several strings within parentheses to have them joined together.’

24
Q

Is there a character type?

A

No. A character is simply a string of size one.

25
How do negative indices work for strings? "Python"[-2] for instance
Start counting from right. This example is o for ex. Since -0 is same as 0, negative indices start from -1
26
How do slicing work for strings? word[0:2]
``` # characters from position 0 (included) to 2 (excluded) 'Py' ```
27
word[-2:]
``` # characters from the second-last (included) to the end 'on' ```
28
word[4:42]
'on'
29
word[42:]
''
30
word[42]
Traceback (most recent call last): File "", line 1, in IndexError: string index out of range
31
``` word[0] = 'J' word[2:] = 'py' ```
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error: TypeError: 'str' object does not support item assignment
32
What operations are supported in lists?
Slicing(shallow copy of list), indexing, concatenation, append
33
Are lists mutable or immutable?
Mutable
34
Assignment to slices
``` >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> # replace some values >>> letters[2:5] = ['C', 'D', 'E'] >>> letters ['a', 'b', 'C', 'D', 'E', 'f', 'g'] >>> # now remove them >>> letters[2:5] = [] >>> letters ['a', 'b', 'f', 'g'] >>> # clear the list by replacing all the elements with an empty list >>> letters[:] = [] >>> letters [] ```
35
which has higher precedence - or **?
** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2.
36
What is PEP 8?
Coding style guide, most important points are; * use no-tabs, use 4 spaces instead * wrap lines not to exceed 79 characters(for display purposes) * use blank lines to separate funcs classes code blocks * when possible put comments on a line of their own * use docstrings * use spaces around operator and commas ex. a = f(1, 2) + g(3, 4) * CamelCase for classes, lower_case_with_underscores for functions and methods * use standart encodings