Basics Flashcards
(36 cards)
where does the name come from
BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles
possible interpreter installation locations
/usr/local/bin/python3.6
/usr/local/python
windows: C:\Python36
exit interpreter
ctrl+ D, ctrl+Z, quit()
what is sys.argv for?
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’]
What does TTY stand for?
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.
What is the default encoding for Python source files?
UTF-8
To declare an encoding other than default one what should one do?
The first line of the file should be as follows: # -*- coding: encoding -*-
Is there any exception to declaring encoding as first line?
Yes, when the source code starts with a UNIX shebang line. #!/usr/bin/env python3 # -*- coding: cp-1252 -*-
What is the comment for Python?.
A line starting with #. But # inside string literal is just a #, not a comment
explain / // %
/ classic division // floor division % returns remainder
explain **
used for power
What happens when you operate a mixture of integer and floating points?
Operands convert the integer operand to floating point.
What _ is used for in interactive mode?
The last printed expression is assigned to the variable _. Its read only. Assigning a value creates a local variable masking it.
What number types are supported?
int, float, Decimal, Fraction, complex numbers.
> > > ‘spam eggs’
> > > ‘doesn't’
> > > “doesn’t”
> > > ‘“Yes,” he said.’
> > > ”"Yes," he said.”
> > > ‘“Isn't,” she said.’
>>> '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.'
> > > ‘“Isn't,” she said.’
> > > print(‘“Isn't,” she said.’)
> > > s = ‘First line.\nSecond line.’
> > > s
> > > print(s)
> > > ‘“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.
> > > print(‘C:\some\name’)
> > > print(r’C:\some\name’)
> > > 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
How do you span string literals multiple lines?
using either ‘'’…’’’ or “"”…”””
if you do not want newlines put \ after each line.
print(“””\
Usage: thingy [OPTIONS]
-h Display this usage message\
-H hostname Hostname to connect to\
“””)
Usage: thingy [OPTIONS]
-h Display this usage message -H hostname Hostname to connect to
print(“””\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
“””)
Usage: thingy [OPTIONS]
- h Display this usage message - H hostname Hostname to connect to
3 * ‘un’ + ‘ium’
‘unununium’
‘Py’ ‘thon’
‘Python’
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.Only works for literals.
text = (‘Put several strings within parentheses ‘
‘to have them joined together.’)
‘Put several strings within parentheses to have them joined together.’
Is there a character type?
No. A character is simply a string of size one.