You are on page 1of 4

3/7/2021 DS_Program_File_5 - Jupyter Notebook

In [16]: # DS Exercise 1.26, random numbers

import random
print(random.randint(1, 10))
print(random.randrange(1, 20))
print(random.randrange(0,5))
print(random.randrange(-20, -10))

5
14
3
-20

In [15]: import numpy

randnums= numpy.random.randint(1,100,5)
print(randnums)

[ 1 92 41 44 67]

In [24]: # DS Exercise 1.27, strings are arrays


a = "Hello, World!"
for x in range(0,len(a)):
print(a[x])

H
e
l
l
o
,

W
o
r
l
d
!

In [25]: a = "Hello, World!"


for x in a:
print(x)

H
e
l
l
o
,

W
o
r
l
d
!

In [26]: txt = "The best things in life are free!"


print("free" in txt)

True

In [27]: txt = "The best things in life are free!"


if "free" in txt:
print("Yes, 'free' is present.")

Yes, 'free' is present.

In [28]: txt = "The best things in life are free!"


print("expensive" not in txt)

True

In [29]: txt = "The best things in life are free!"


if "expensive" not in txt:
print("Yes, 'expensive' is NOT present.")

Yes, 'expensive' is NOT present.

localhost:8888/notebooks/DS_Program_File_5.ipynb# 1/4
3/7/2021 DS_Program_File_5 - Jupyter Notebook

In [30]: # DS Exercise 1.28, boolean


print(10 > 9)
print(10 == 9)
print(10 < 9)

True
False
False

In [40]: a = 200
b = 20

if b > a:
print("b is greater than a")
elif b < a:
print("b is not greater than a")
else:
print("b is equal to a")

b is not greater than a

In [59]: # evaluate variables

x = "Hello"
y = 15
z=0
s=-10
b=" "
r="" # empty string

print(bool(x))
print(bool(y))
print(bool(z))
print(bool(s))
print(bool(b))
print(bool(r))

True
True
False
True
True
False

In [48]: bool("abc")

Out[48]: True

In [49]: bool(123)

Out[49]: True

In [50]: bool(["apple", "cherry", "banana"])

Out[50]: True

In [51]: bool(False)

Out[51]: False

In [52]: bool(None)

Out[52]: False

In [53]: bool(())

Out[53]: False

In [54]: bool([])

Out[54]: False

In [55]: bool({})

Out[55]: False

In [60]: x = 200
print(isinstance(x, int)) #chk if an object is integer or not

True

localhost:8888/notebooks/DS_Program_File_5.ipynb# 2/4
3/7/2021 DS_Program_File_5 - Jupyter Notebook

In [76]: # DS Exercise 1.29, operators

print(10 + 5)

15

In [62]: print(10 - 5)

In [63]: print(10 * 5)

50

In [72]: print(12 / 5)

2.4

In [67]: print(10 % 5) # modulus

In [68]: print(10 ** 3) # exponentiation

1000

In [73]: print(12 // 5) #floor division

In [78]: print(3 & 3) # bitwise and

In [80]: print(2 | 3) # bitwise or

In [81]: print(2 ^ 3) # bitwise xor

In [82]: print(~2) #invert all bits

-3

In [83]: print(5 == 5)

True

In [84]: print(2 != 5)

True

In [85]: print(5 >= 5)

True

In [86]: print(2<= 5)

True

In [88]: print(2 < 5 and 4< 10) # logical and

True

In [89]: print(2 < 5 or 7 < 4) # logical or

True

In [90]: not(2 < 5 and 3 < 10) # logical not

Out[90]: False

localhost:8888/notebooks/DS_Program_File_5.ipynb# 3/4
3/7/2021 DS_Program_File_5 - Jupyter Notebook

In [102]: # membership operators, in and not in


x="abcd"
if 'a' in x:
print(" a is really in x")
if 'e' not in x:
print(" e is really not in x")

a is really in x
e is really not in x

In [103]: # identity operators , is and is not


string1="abc"
string2="xyz"
print(string1 is string2)
print(string1 is not string2)

False
True

In [ ]:

localhost:8888/notebooks/DS_Program_File_5.ipynb# 4/4

You might also like