nums = [1, 2, 3, 4, 5, 6]Module I - Day 2
Python Foundation
Nov-Jan 2025 batch, Vikrant Patil
Date: 18 Nov 2025
Click here for All Notes
© Vikrant Patil
more built ins
len(nums)6
int("556")556
int(5.6)5
float("78.5")78.5
float(78)78.0
str(78.8)'78.8'
str(nums)'[1, 2, 3, 4, 5, 6]'
sum(nums)21
max(nums)6
min(nums)1
sorted(nums) # this creates a new list with numbers in sorted order[1, 2, 3, 4, 5, 6]
nums = [4 , 3, 1, 7, 3, 9, 2]sorted(nums) # it will sort and created new list , but original list remains as it is[1, 2, 3, 3, 4, 7, 9]
nums[4, 3, 1, 7, 3, 9, 2]
new_nums = [2, 3, 5, 1, 6, 7]I restarted the kernel here so I lost all the variable that i had defined
nums--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 1 ----> 1 nums NameError: name 'nums' is not defined
nums = [4 , 3, 1, 7, 3, 9, 2]sorted(nums)[1, 2, 3, 3, 4, 7, 9]
sorted(nums, reverse=True) # reverse is called as named argument. it menas you have to give it always with name[9, 7, 4, 3, 3, 2, 1]
there is another basic datatype which we didn’t talk… boolean it has two values True and False
sum(nums)/len(nums)4.142857142857143
TrueTrue
FalseFalse
Everythng in Python is an object! it means functions, classes, integers , floats..everythings is treated in same fashion when it comes to variable
x = 10
y = x # alias to xmylen = len # alias to len!mylen(nums)7
methods
A function defined inside the object which can manipulate the object’s data is called as methods.
empty = [] empty.append(0) # a funempty[0]
empty.append(1)empty[0, 1]
empty.insert(0, 4)empty[4, 0, 1]
x = 10
y = x
x = 30nums[4, 3, 1, 7, 3, 9, 2]
nums[0] = -1nums.append(5)empty.pop() # remove last item and retun it!1
empty[4, 0]
x = empty.pop() # this will remove last item from empty and store it in xx0
empty[4]
help(empty.pop)Help on built-in function pop:
pop(index=-1, /) method of builtins.list instance
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
nums[-1, 3, 1, 7, 3, 9, 2, 5]
nums.pop(0)-1
nums[3, 1, 7, 3, 9, 2, 5]
nums.insert(2, -5) # first argument is index at which we want to insert and second arg is the value to be insertednums[3, 1, -5, 2, 7, 3, 9, 2, 5]
[1, 2, 3, 4] + [-1, -1, -1][1, 2, 3, 4, -1, -1, -1]
ones = [1]*5
twos = [2]*5ones[1, 1, 1, 1, 1]
twos[2, 2, 2, 2, 2]
ones + twos[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
ones [1, 1, 1, 1, 1]
twos[2, 2, 2, 2, 2]
ones.extend(twos)ones[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
twos[2, 2, 2, 2, 2]
string methods
text = "Some text for testing methods"text.capitalize()'Some text for testing methods'
name = "vikrant"name.capitalize()'Vikrant'
name.upper()'VIKRANT'
text.startswith("hello")False
text'Some text for testing methods'
text.lower()'some text for testing methods'
text.endswith("methods")True
text.strip()'Some text for testing methods'
traling = " this text has trailing white spaces "traling' this text has trailing white spaces '
traling.strip()'this text has trailing white spaces'
x # iterpreters help to developer ... show me what is this object0
print(x)0
mulitline = """this is line one
this is line 2"""print(mulitline)this is line one
this is line 2
mulitline'this is line one\nthis is line 2'
line = "this is a line from csv file\n"line.strip() # removes trailing whitespaces ..space, tab, new line'this is a line from csv file'
text'Some text for testing methods'
text.split() # by default it will split on white space['Some', 'text', 'for', 'testing', 'methods']
text.split() # this will create list of wrods ... assumption is words are separated by white spaces!['Some', 'text', 'for', 'testing', 'methods']
header = "symbol,value,high value,low value,volume"columnsnames = header.split(",")columnsnames['symbol', 'value', 'high value', 'low value', 'volume']
if list of words is given how do you make statement out of it?
words = ["one", "two", "three", "four", "five"]" ".join(words)'one two three four five'
statement = " ".join(words)say_text = 'hello, I want to say something... and it it "python"'say_text'hello, I want to say something... and it it "python"'
say_text.split()['hello,',
'I',
'want',
'to',
'say',
'something...',
'and',
'it',
'it',
'"python"']
say_text.split(",")['hello', ' I want to say something... and it it "python"']
say_text.split(".")['hello, I want to say something', '', '', ' and it it "python"']
words['one', 'two', 'three', 'four', 'five']
" ".join(words)'one two three four five'
"".join(words)'onetwothreefourfive'
"/".join(words)'one/two/three/four/five'
",".join(words)'one,two,three,four,five'
problems
- a filename is given is a variable
filename. how will you find extension of that file?
filename = "hello.txt"filename.split(".")['hello', 'txt']
filename.split(".")[-1]'txt'
list slicing
nums[3, 1, -5, -5, 2, 7, 3, 9, 2, 5]
nums[0]3
nums[1]1
nums[1:5] # start at index 1 and end at index 5(exluded!)[1, -5, -5, 2]
range(10)range(0, 10)
naturals = list(range(10))naturals [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
natural[5:] # what ever yoi don't give it will default[5, 6, 7, 8, 9]
naturals[3:] # drop first 3..and give rest[3, 4, 5, 6, 7, 8, 9]
naturals[:3] # take first three[0, 1, 2]
naturals[::] # copy[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
naturals[::2] [0, 2, 4, 6, 8]
natural[1:8:3] # start:end:step[1, 4, 7]
natural[::-1] # reverse[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
x = 10
y = x # this is not copy. linking the ones = [1, 1, 1]
copy_ones = onescopy_ones[1, 1, 1]
ones.append(0)ones[1, 1, 1, 0]
copy_ones[1, 1, 1, 0]
real_copy = ones[::]real_copy[1, 1, 1, 0]
ones.pop()0
ones[1, 1, 1]
real_copy[1, 1, 1, 0]
ones[1, 1, 1]
copy_ones[1, 1, 1]
copy_ones.append(-1)copy_ones[1, 1, 1, -1]
ones[1, 1, 1, -1]
real_copy[1, 1, 1, 0]
it_is_ok_to_hav_big_name_but_meaningful = 1list(range(1, 9, 2)) # start, end, step[1, 3, 5, 7]
list(range(10)) # only end! start is taken as 0 and step as 1[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(1,10))[1, 2, 3, 4, 5, 6, 7, 8, 9]
functions
def square(x): # code block start from next line
s = x * x
return sthe moment above cell is executed, there will be a variable with name square created!
square<function __main__.square(x)>
square()--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[140], line 1 ----> 1 square() TypeError: square() missing 1 required positional argument: 'x'
square(5)25
def say_hello(name):
print("Hello", name)say_hello("python")Hello python
square(5)25
def foo():
pass # empty statmentfoo<function __main__.foo()>
foo()any function that does not return , it actually returns None object
del sqaure # you delete variables using del statementsqr5 = square(5)x = 10sqr525
val = say_hello("vikrant")Hello vikrant
valvalprint(val)None
valdef square(x): # code block start from next line
s = x * x
return sx = 10square(56)3136
def func(a):
a = 50a = 10
func(a)a10
x = 10
x = 20Homework
Write a function
count_digitswhich will return number of digits from given integer.Write a function
sum_naturalswhich will return sum of first n natural numbers. hint: make use of range and sumWrite a function
symmetric_joinwhich will join a given list with its own mirror. Here is examplesymmetric_join([1,2,34]) [1, 2, 34, 34, 2, 1]