Professional Documents
Culture Documents
Notes
Notes
PEMDAS
Plz excuse my dear an shell
>>> 21/3
7.0
>>> 21/4
5.25
>>> 21//4
5
----------------------------------------------------------------------------------------------------------------------------------------TYPE CASTING STRING INPUT TO INT
The easiest way to obtain user input from the command-line is with the
input() : The INPUT IS TAKEN AS STRING AND NEED TO BE CASTED FOR
STRING TO FLOAT OR INT.
built-in function.
It reads from standard input and assigns the string value to the variable you
designate.
You can use the
int()
built-in function (Python versions older than 1.5 will have to use the string.atoi()
function)
to convert any numeric input string to an integer representation.
>>> user = raw_input('Enter login name: ')
Enter login name: root
>>> print 'Your login is:',
user Your login is: root
The above example was strictly for text input.
A numeric string input (with conversion to a real integer) example follows below:
>>> num = input('Now enter a number: ')
Now enter a number: 1024
>>> print 'Doubling your number: %d' % (int(num) * 2)
Doubling your number: 2048
>>> num = int ( input('Now enter a number: '))
BUILT IN FUNCTION
>>> dir (__builtins__)
dir (__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning',
'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning',
'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError',
'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError',
'OSError', 'OverflowError', 'PendingDeprecationWarning',
'PermissionError', 'ProcessLookupError', 'ReferenceError',
'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration',
'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
'TimeoutError', 'True', 'TypeError', 'UnboundLocalError',
'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError',
'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__',
'__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__',
'__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes',
'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter',
'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help',
'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list',
'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open',
'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round',
to see all built in modules type " help( ' modules ' ) "
to see all functions inside a module type
" help('moduleName') "
ex- help('math')
Variable Assignment
Equal sign ( = ) is the assignment operator
A variable is like a mailbox. You can put something in the mailbox and check the
contents later. Putting a new value in the mailbox replaces the existing value.
Computer memory is really a very large collection of such boxes. For instance,
256MB of RAM has about 67 million boxes for storing numbers.
Python (and almost all languages) make it easier for us to remember what each box
holds by giving it a name (called an identifier). It is much easier to remember a
variable with a name rather than the fact that box number 4168443356 is storing
the data we want.
Python variables are created by assignment, using the = operator, which is the
process of setting the value of a variable (putting data in the mailbox). Any other
mention of the variable name retrieves the value stored in the variable .
Also, if you familiar with C, you are aware that assignments are treated as
expressions. This is not the case for Python, where assignments do not
have inherent values. Statements such as the following are invalid in
Python:
>>> x = 1
>>> y = (x = x + 1)
Beginning in Python 2.0, the equals sign can be combined with an arithmetic
operation and the resulting value reassigned to the existing variable. Known as
augmented assignment,
x=x+1
x += 1
Python does not support pre-/post-increment nor pre-/post-decrement
operators such as x++ or --x.
Multiple Assignment
>>> x = y = z = 1
"Multuple" Assignment
we use "multuple" here because when assigning variables this way, the objects on
both sides of the equals sign are tuples
>>> x, y, z = 1, 2, 'a string'
>>> x
1
>>> y
2
>>> z
'a string'
>>> (x, y, z) = (1, 2, 'a string')
>>> y
6220180019400020533
June2006
0031
4890211523129540 corporation
3/13 3/23
5988