Tutorial 1
Tutorial 1
1
Data Type Example
Number Integer x=4
Long integer x = 15L
Floating point x = 3.142
Boolean x = True
Text Character x = ‘c’
String x = “this” or x = ‘this’
[ ]: x = 4 # integer
print(x, type(x))
The following are some of the arithmetic operations available for manipulating integers and float-
ing point numbers
[ ]: x = 4 # integer
x1 = x + 4 # addition
x2 = x * 3 # multiplication
x += 2 # equivalent to x = x + 2
x3 = x
x *= 3 # equivalent to x = x * 3
x4 = x
x5 = x % 4 # modulo (remainder) operator
print(x,x1,x2,x3,x4,x5)
print(z,z1,z2,z3,z4)
print(z5,z6,z7,z8)
The following are some of the functions provided by the math module for integers and floating
2
point numbers
[ ]: import math
x = 4
print(math.sqrt(x)) # sqrt(4) = 2
print(math.pow(x,2)) # 4**2 = 16
print(math.exp(x)) # exp(4) = 54.6
print(math.log(x,2)) # log based 2 (default is natural logarithm)
print(math.fabs(-4)) # absolute value
print(math.factorial(x)) # 4! = 4 x 3 x 2 x 1 = 24
z = 0.2
print(math.ceil(z)) # ceiling function
print(math.floor(z)) # floor function
print(math.trunc(z)) # truncate function
x = math.inf # infinity
print(math.isinf(x))
The following are some of the logical operations available for booleans
[ ]: y1 = True
y2 = False
The following are some of the operations and functions for manipulating strings
[ ]: s1 = "This"
s2 = "This is a string"
words = s2.split(' ') # split the string into words
3
print(words[0])
print(s2.replace('a','another')) # replace "a" with "another"
print(s2.replace('is','at')) # replace "is" with "at"
print(s2.find("a")) # find the position of "a" in s2
print(s1 in s2) # check if s1 is a substring of s2
[ ]: intlist = [1, 3, 5, 7, 9]
print(type(intlist))
print(intlist)
intlist2 = list(range(0,10,2)) # range[startvalue, endvalue, stepsize]
print(intlist2)
4
separator = " "
print(separator.join(mylist)) # merge all elements of the list into a string
The following examples show how to create and manipulate a dictionary object
[ ]: abbrev = {}
abbrev['MI'] = "Michigan"
abbrev['MN'] = "Minnesota"
abbrev['TX'] = "Texas"
abbrev['CA'] = "California"
print(abbrev)
print(abbrev.keys()) # get the keys of the dictionary
print(abbrev.values()) # get the values of the dictionary
print(len(abbrev)) # get number of key-value pairs
print(abbrev.get('MI'))
print("FL" in abbrev)
print("CA" in abbrev)
The following examples show how to create and manipulate a tuple object. Unlike a list, a tuple
object is immutable, i.e., they cannot be modified after creation.
print(MItuple)
print(MItuple[1:])
5
print(states[2][1:])
print(states)
x = 10
if x % 2 == 0:
print("x =", x, "is even")
else:
print("x =", x, "is odd")
if x > 0:
print("x =", x, "is positive")
elif x < 0:
print("x =", x, "is negative")
else:
print("x =", x, "is neither positive nor negative")
6
fruits = {'apples': 3, 'oranges': 4, 'bananas': 2, 'cherries': 10}
fruitnames = [k for (k,v) in fruits.items()]
print(fruitnames)
mylist = list(range(-10,10))
print(mylist)
i = 0
while (mylist[i] < 0):
i = i + 1
print(myfunc(2))
[ ]: import math
if sortFlag:
outlist.sort()
return outlist
print(discard(mylist,True))
7
[ ]: states = [('MI', 'Michigan', 'Lansing'),('CA', 'California', 'Sacramento'),
('TX', 'Texas', 'Austin'), ('MN', 'Minnesota', 'St Paul')]
print('State=',fields[1],'(',fields[0],')','Capital:', fields[2])