Lecture 05
Lecture 05
In [35]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 4
the number is even
In [36]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 4.3
Traceback (most recent call last):
File "C:\Program
Files\Spyder\pkgs\spyder_kernels\py3compat.py", line
356, in compat_exec
exec(code, globals, locals)
File "c:\users\amajid\downloads\untitled3.py",
line 8, in <module>
x=int(input('plz enter an int: '))
In [37]: '123'.isdigit()
Out[37]: True
In [38]: '123a'.isdigit()
Out[38]: False
1
In [39]: help(str.isdigit)
Help on method_descriptor:
isdigit(self, /)
Return True if the string is a digit string,
False otherwise.
In [39]:
In [40]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 2.3
the entered input is invalid
In [41]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 4
Traceback (most recent call last):
File "C:\Program
Files\Spyder\pkgs\spyder_kernels\py3compat.py", line
356, in compat_exec
exec(code, globals, locals)
File "c:\users\amajid\downloads\untitled3.py",
line 15, in <module>
if x%2==0:
2
TypeError: not all arguments converted during string
formatting
In [42]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 3
the number is odd
In [43]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: 6
the number is even
In [44]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter an int: fgfd
the entered input is invalid
In [45]: x='2'
In [46]: type(x)==int
Out[46]: False
In [47]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter your marks: 90
you get an A
In [48]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter your marks: 40
You get an F
3
In [49]: runfile('C:/Users/amajid/Downloads/
untitled3.py', wdir='C:/Users/amajid/Downloads')
plz enter your marks: 60.5
you get a D
In [50]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
plz enter your age: 50
plz enter your membership status(yes/no): No
no discount
In [51]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
plz enter your age: 40
plz enter your membership status(yes/no): yes
you get regular discount
In [52]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
plz enter your age: -3
plz enter your membership status(yes/no): yes
age is incorrect
In [55]: help(random)
Help on module random:
NAME
random
CLASSES
_random.Random(builtins.object)
Random
SystemRandom
class Random(_random.Random)
| Random(x=None)
|
| Random() -> create a random number generator
with its own internal state.
|
| Method resolution order:
| Random
| _random.Random
| builtins.object
|
| Methods defined here:
|
| __getstate__(self)
|
| __init__(self, x=None)
| Initialize self. See help(type(self))
for accurate signature.
|
5
| __reduce__(self)
| Helper for pickle.
|
| __setstate__(self, state)
|
| betavariate(self, alpha, beta)
|
| choice(self, seq)
|
| choices(self, population, weights=None, *,
cum_weights=None, k=1)
|
| expovariate(self, lambd)
|
| gammavariate(self, alpha, beta)
|
| gauss(self, mu, sigma)
|
| getstate(self)
| getstate() -> tuple containing the
current state.
|
| lognormvariate(self, mu, sigma)
|
| normalvariate(self, mu, sigma)
|
| paretovariate(self, alpha)
|
| randint(self, a, b)
|
| randrange(self, start, stop=None, step=1,
_int=<class 'int'>)
|
| sample(self, population, k)
6
|
| seed(self, a=None, version=2)
| seed([n]) -> None.
|
| Defaults to use urandom and falls back
to a combination
| of the current time and the process
identifier.
|
| setstate(self, state)
| setstate(state) -> None. Restores
generator state.
|
| shuffle(self, x, random=None)
|
| triangular(self, low=0.0, high=1.0,
mode=None)
|
| uniform(self, a, b)
|
| vonmisesvariate(self, mu, kappa)
|
| weibullvariate(self, alpha, beta)
|
|
----------------------------------------------------
------------------
| Class methods defined here:
|
| __init_subclass__(**kwargs) from
builtins.type
| This method is called when a class is
subclassed.
|
7
| The default implementation does nothing.
It may be
| overridden to extend subclasses.
|
|
----------------------------------------------------
------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if
defined)
|
| __weakref__
| list of weak references to the object
(if defined)
|
|
----------------------------------------------------
------------------
| Data and other attributes defined here:
|
| VERSION = 3
|
|
----------------------------------------------------
------------------
| Methods inherited from _random.Random:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| getrandbits(self, k, /)
| getrandbits(k) -> x. Generates an int
8
with k random bits.
|
| random(self, /)
| random() -> x in the interval [0, 1).
|
|
----------------------------------------------------
------------------
| Static methods inherited from
_random.Random:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See
help(type) for accurate signature.
class SystemRandom(Random)
| SystemRandom(x=None)
|
| Random() -> create a random number generator
with its own internal state.
|
| Method resolution order:
| SystemRandom
| Random
| _random.Random
| builtins.object
|
| Methods defined here:
|
| getrandbits(self, k)
| getrandbits(k) -> x. Generates an int
with k random bits.
|
| getstate = _notimplemented(self, *args,
9
**kwds)
|
| random(self)
| random() -> x in the interval [0, 1).
|
| seed(self, *args, **kwds)
| seed([n]) -> None.
|
| Defaults to use urandom and falls back
to a combination
| of the current time and the process
identifier.
|
| setstate = _notimplemented(self, *args,
**kwds)
|
|
----------------------------------------------------
------------------
| Methods inherited from Random:
|
| __getstate__(self)
|
| __init__(self, x=None)
| Initialize self. See help(type(self))
for accurate signature.
|
| __reduce__(self)
| Helper for pickle.
|
| __setstate__(self, state)
|
| betavariate(self, alpha, beta)
|
10
| choice(self, seq)
|
| choices(self, population, weights=None, *,
cum_weights=None, k=1)
|
| expovariate(self, lambd)
|
| gammavariate(self, alpha, beta)
|
| gauss(self, mu, sigma)
|
| lognormvariate(self, mu, sigma)
|
| normalvariate(self, mu, sigma)
|
| paretovariate(self, alpha)
|
| randint(self, a, b)
|
| randrange(self, start, stop=None, step=1,
_int=<class 'int'>)
|
| sample(self, population, k)
|
| shuffle(self, x, random=None)
|
| triangular(self, low=0.0, high=1.0,
mode=None)
|
| uniform(self, a, b)
|
| vonmisesvariate(self, mu, kappa)
|
| weibullvariate(self, alpha, beta)
11
|
|
----------------------------------------------------
------------------
| Class methods inherited from Random:
|
| __init_subclass__(**kwargs) from
builtins.type
| This method is called when a class is
subclassed.
|
| The default implementation does nothing.
It may be
| overridden to extend subclasses.
|
|
----------------------------------------------------
------------------
| Data descriptors inherited from Random:
|
| __dict__
| dictionary for instance variables (if
defined)
|
| __weakref__
| list of weak references to the object
(if defined)
|
|
----------------------------------------------------
------------------
| Data and other attributes inherited from
Random:
|
12
| VERSION = 3
|
|
----------------------------------------------------
------------------
| Methods inherited from _random.Random:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
|
----------------------------------------------------
------------------
| Static methods inherited from
_random.Random:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See
help(type) for accurate signature.
FUNCTIONS
betavariate(alpha, beta) method of Random
instance
choices(population, weights=None, *,
cum_weights=None, k=1) method of Random instance
13
gauss(mu, sigma) method of Random instance
DATA
__all__ = ['Random', 'seed', 'random',
'uniform', 'randint', 'choice',...
FILE
c:\program
files\spyder\python\python38.zip\random.py
In [59]: randint(1,10)
Out[59]: 3
In [60]: randint(1,10)
Out[60]: 1
In [61]: randint(1,10)
Out[61]: 8
In [62]: randint(1,10)
Out[62]: 6
In [63]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
rock
In [64]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
rock
In [65]: runfile('C:/Users/amajid/Downloads/
untitled2.py', wdir='C:/Users/amajid/Downloads')
scissor
In [66]:
16