0% found this document useful (0 votes)
5 views9 pages

py 4

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views9 pages

py 4

Uploaded by

fortiratra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Section 1.

7: User Input

Interactive input

To get input from the user, use the input function (note: in Python 2.x, the function is called
raw_input instead,

although Python 2.x has its own version of input that is completely different):

Python 2.x Version ≥ 2.3

name = raw_input("What is your name? ")

# Out: What is your name? _

Security Remark Do not use input() in Python2 - the entered text will be evaluated as if it were a

Python expression (equivalent to eval(input()) in Python3), which might easily become a


vulnerability.

See this article for further information on the risks of using this function.

Python 3.x Version ≥ 3.0

name = input("What is your name? ")

# Out: What is your name? _

The remainder of this example will be using Python 3 syntax.

The function takes a string argument, which displays it as a prompt and returns a string. The above
code provides a

prompt, waiting for the user to input.

name = input("What is your name? ")

# Out: What is your name?

If the user types "Bob" and hits enter, the variable name will be assigned to the string "Bob":

name = input("What is your name? ")

# Out: What is your name? Bob

print(name)

# Out: Bob

Note that the input is always of type str, which is important if you want the user to enter numbers.
Therefore, you

need to convert the str before trying to use it as a number:

x = input("Write a number:")

# Out: Write a number: 10

x/2
# Out: TypeError: unsupported operand type(s) for /: 'str' and 'int'

float(x) / 2

# Out: 5.0

NB: It's recommended to use try/except blocks to catch exceptions when dealing with user inputs.
For instance, if

your code wants to cast a raw_input into an int, and what the user writes is uncastable, it raises a
ValueError.

Section 1.8: Built in Modules and Functions

A module is a file containing Python definitions and statements. Function is a piece of code which
execute some

logic.

>>> pow(2,3) #8

GoalKicker.com – Python® Notes for Professionals 22

To check the built in function in python we can use dir(). If called without an argument, return the
names in the

current scope. Else, return an alphabetized list of names comprising (some of) the attribute of the
given object, and

of attributes reachable from it.

>>> dir(__builtins__)

'ArithmeticError',

'AssertionError',

'AttributeError',

'BaseException',

'BufferError',

'BytesWarning',

'DeprecationWarning',

'EOFError',

'Ellipsis',

'EnvironmentError',

'Exception',

'False',

'FloatingPointError',
'FutureWarning',

'GeneratorExit',

'IOError',

'ImportError',

'ImportWarning',

'IndentationError',

'IndexError',

'KeyError',

'KeyboardInterrupt',

'LookupError',

'MemoryError',

'NameError',

'None',

'NotImplemented',

'NotImplementedError',

'OSError',

'OverflowError',

'PendingDeprecationWarning',

'ReferenceError',

'RuntimeError',

'RuntimeWarning',

'StandardError',

'StopIteration',

'SyntaxError',

'SyntaxWarning',

'SystemError',

'SystemExit',

'TabError',

'True',

'TypeError',

'UnboundLocalError',
'UnicodeDecodeError',

'UnicodeEncodeError',

'UnicodeError',

'UnicodeTranslateError',

'UnicodeWarning',

'UserWarning',

'ValueError',

'Warning',

'ZeroDivisionError',

'__debug__',

'__doc__',

GoalKicker.com – Python® Notes for Professionals 23

'__import__',

'__name__',

'__package__',

'abs',

'all',

'any',

'apply',

'basestring',

'bin',

'bool',

'buffer',

'bytearray',

'bytes',

'callable',

'chr',

'classmethod',

'cmp',

'coerce',

'compile',
'complex',

'copyright',

'credits',

'delattr',

'dict',

'dir',

'divmod',

'enumerate',

'eval',

'execfile',

'exit',

'file',

'filter',

'float',

'format',

'frozenset',

'getattr',

'globals',

'hasattr',

'hash',

'help',

'hex',

'id',

'input',

'int',

'intern',

'isinstance',

'issubclass',

'iter',

'len',

'license',
'list',

'locals',

'long',

'map',

'max',

'memoryview',

'min',

'next',

'object',

'oct',

'open',

'ord',

GoalKicker.com – Python® Notes for Professionals 24

'pow',

'print',

'property',

'quit',

'range',

'raw_input',

'reduce',

'reload',

'repr',

'reversed',

'round',

'set',

'setattr',

'slice',

'sorted',

'staticmethod',

'str',

'sum',
'super',

'tuple',

'type',

'unichr',

'unicode',

'vars',

'xrange',

'zip'

To know the functionality of any function, we can use built in function help .

>>> help(max)

Help on built-in function max in module __builtin__:

max(...)

max(iterable[, key=func]) -> value

max(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its largest item.

With two or more arguments, return the largest argument.

Built in modules contains extra functionalities. For example to get square root of a number we need
to include math

module.

>>> import math

>>> math.sqrt(16) # 4.0

To know all the functions in a module we can assign the functions list to a variable, and then print
the variable.

>>> import math

>>> dir(math)

['__doc__', '__name__', '__package__', 'acos', 'acosh',

'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign',

'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1',

'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma',

'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10',

'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt',


'tan', 'tanh', 'trunc']

it seems __doc__ is useful to provide some documentation in, say, functions

GoalKicker.com – Python® Notes for Professionals 25

>>> math.__doc__

'This module is always available. It provides access to the\nmathematical

functions defined by the C standard.'

In addition to functions, documentation can also be provided in modules. So, if you have a file named

helloWorld.py like this:

"""This is the module docstring."""

def sayHello():

"""This is the function docstring."""

return 'Hello World'

You can access its docstrings like this:

>>> import helloWorld

>>> helloWorld.__doc__

'This is the module docstring.'

>>> helloWorld.sayHello.__doc__

'This is the function docstring.'

For any user defined type, its attributes, its class's attributes, and recursively the attributes of its
class's base

classes can be retrieved using dir()

>>> class MyClassObject(object):

... pass

...

>>> dir(MyClassObject)

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__',

'__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',

'__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Any data type can be simply converted to string using a builtin function called str. This function is
called by default

when a data type is passed to print

>>> str(123) # "123"


Section 1.9: Creating a module

A module is an importable file containing definitions and statements.

A module can be created by creating a .py file.

# hello.py

def say_hello():

print("Hello!")

Functions in a module can be used by importing the module.

For modules that you have made, they will need to be in the same directory as the file that you are
importing them

into. (However, you can also put them into the Python lib directory with the pre-included modules,
but should be

avoided if possible.)

$ python

>>> import hello

>>> hello.say_hello()

You might also like