instructions to execute jupyter notebook code
Question: what do programs do?
Programs mainly manupulate or process data
0 # integers
0
42
42
42 + 42 # some kind opeartors that will allow doing arithmatics operations
84
42**2 # power
1764
42**100 # you don't how to store big numbers
2113143741011360736530044045523113991698878330713580061264477934391564919875497777688215057732151811172029315247932158994879668553186145824710950394684126712037376
x = 42 # you can save data inside a variable called x
x + 2
44
A not for c/java programmers, in python we don't declare variable and their types. We create variable at any point in program and you can use it to store any kind of data.
x
42
x = 4.2 # float
x + 2.0
6.2
42/2 # division opeartors, results in float even if numerator and deniminator are integers
21.0
42//2 # integers division
21
43//2
21
43/2
21.5
print(x)
4.2
x
4.2
x = 42
y = 45
x*x
y*y
x*x + y*y # only result of this line will be shown below
3789
x = 42
y = 45
print(x*x)
print(y*y)
print(x*x + y*y)
1764 2025 3789
There is no special data type for charecters. There is only string to represent text data
first = "Rupali"
second = "Rupak"
print(first)
Rupali
"any thing that you write inside quotes is text data"
'any thing that you write inside quotes is text data'
first + second # concatenate
'RupaliRupak'
first*5 # repeat it five time
'RupaliRupaliRupaliRupaliRupali'
"="*100
'===================================================================================================='
"Rupali" # litteral string
'Rupali'
"Rupali" "Rupak" "is" "her" "name"
'RupaliRupakishername'
first second
File "<ipython-input-32-0ddb5fdc36bf>", line 1 first second ^ SyntaxError: invalid syntax
"text" "another text"
'textanother text'
'this is also a string'
'this is also a string'
"He said, 'I am fine'"
"He said, 'I am fine'"
first[0] # it gives array like access to access individual chars
'R'
single = "He said, 'I am fine'"
single[9]
"'"
single[10]
'I'
'you can also have " inside a string'
'you can also have " inside a string'
multiline = """first line
second line
third line"""
print(multiline)
first line second line third line
multiline # you will see newline character
'first line\nsecond line\nthird line'
multi = "first line\nsecond line\nthird line"
students = ["Aparna", "Ana", "Arpita", "Akanksha"]
students
['Aparna', 'Ana', 'Arpita', 'Akanksha']
students[0] # gives us array like access
'Aparna'
students[2]
'Arpita'
students[-1] # magical indexing , which allows to access in reverse fashion
'Akanksha'
students[-2]# second last item
'Arpita'
roll_numbers = [42, 43, 44, 45]
mixed = ["some","words", 34, 43]
mixed[-1]
43
mixed[0]
'some'
list_of_list = [["hello","world"], [23, 24]]
list_of_list[0]
['hello', 'world']
list_of_list[1]
[23, 24]
Matrices or multidimensional lists are possible with lists
matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix[0]
[1, 2, 3]
matrix[-1]
[7, 8, 9]
matrix[0][-1]
3
Lists are mutable
mixed
['some', 'words', 34, 43]
mixed[0] = "zero"
mixed
['zero', 'words', 34, 43]
point = (0, 0, 6) # tuple... while creating it you put a round bracket instead of square
point
(0, 0, 6)
point[0]
0
point[-1]
6
point[0] = -1 # tuple is immutable
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-75-039bc8f68d9f> in <module> ----> 1 point[0] = -1 # tuple is immutable TypeError: 'tuple' object does not support item assignment
Dictionary
Named collection of items
roll_numbers = {"Aparna":42, "Ankita":45, "Arpita":35}
roll_numbers
{'Aparna': 42, 'Ankita': 45, 'Arpita': 35}
roll_numbers['Aparna']
42
roll_numbers['Arpita']
35
roll_numbers = {"Arpana":42,
"Ankita": 43,
"Arpita": 35}
roll_numbers
{'Arpana': 42, 'Ankita': 43, 'Arpita': 35}
students_marks = {"Arpita": [95, 93, 90],
"Akshat": [98, 97, 89]}
students_marks
{'Arpita': [95, 93, 90], 'Akshat': [98, 97, 89]}
students_marks['Akshat']
[98, 97, 89]
students_marks['Akansha'] = [89,91,100]
students_marks
{'Arpita': [95, 93, 90], 'Akshat': [98, 97, 89], 'Akansha': [89, 91, 100]}
len(mixed) # handy function to find length of any collection
4
len("this is a string")
16
len(students_marks)
3
len([5, 6, 7, 7])
4
len((6 , 7, 8, 9))
4
str(54)
'54'
x = input("Please enter value of x:")
x + 5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-98-9e8324a7b754> in <module> ----> 1 x + 5 TypeError: can only concatenate str (not "int") to str
x
'45'
int(x)
45
y = int(input("Enter integer value for y:"))
y
565
y + 5
570
roll_numbers
{'Arpana': 42, 'Ankita': 43, 'Arpita': 35}
list(roll_numbers) # will dictionary to list by taking only names
['Arpana', 'Ankita', 'Arpita']
list("alphabets")
['a', 'l', 'p', 'h', 'a', 'b', 'e', 't', 's']
range(10) # will create intergers from 0 to 9
range(0, 10)
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(5,10)) #range(start, end)...make integers from start till end-1
[5, 6, 7, 8, 9]
sum([3,4, 5, 7])
19
max([45, 465, 23, 5])
465
min([3, 4, 5, 6])
3
Questions
42**10042**100
2113143741011360736530044045523113991698878330713580061264477934391564919875497777688215057732151811172029315247932158994879668553186145824710950394684126712037376
len(42**100) # will not work
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-117-88f7b4d6374a> in <module> ----> 1 len(42**100) # will not work TypeError: object of type 'int' has no len()
str_result = str(42**100)
str_result
'2113143741011360736530044045523113991698878330713580061264477934391564919875497777688215057732151811172029315247932158994879668553186145824710950394684126712037376'
len(str_result)
163
float(42**100) # will not give direct answer but you can guess by looking at result
2.113143741011361e+162
range(501, 1001)
range(501, 1001)
numbers = list(range(501, 1001))
numbers[0]
501
numbers[1]
502
numbers[-1]
1000
sum(numbers)
375250