What will learn today?
online notes for this session
len([1,2,3,45])
4
len("test")
4
len({"a":23,"b":34})
2
str(54565)
'54565'
z = int(input("Eneter value of z:"))
def foo(name): # defination function should begin with def, should end :
print("Hello", name)
File "<ipython-input-7-1784cac0f0f9>", line 2 print("Hello", name) ^ IndentationError: expected an indented block
def foo(name): # defination function should begin with def, should end :
print("Hello", name)
foo("Vikrant")
Hello Vikrant
foo("PCCOE")
Hello PCCOE
def area_circle(radius):
return 3.14*radius**2
area_circle(1)
3.14
def twice(x):
return 2*x
def twice1(x):
print(2*x)
twice(5)
10
twice1(5)
10
x_2 = twice(5)
print(x_2)
10
x_2_ = twice1(5)
10
print(x_2_)
None
twice(twice(5))
20
twice1(twice1(5))
10
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-24-9c16ec1a24f8> in <module> ----> 1 twice1(twice1(5)) <ipython-input-14-22e986a8fae6> in twice1(x) 1 def twice1(x): ----> 2 print(2*x) TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
len(str(42**100))
163
x = 15
x
15
x + 3
18
foo
<function __main__.foo(name)>
x
15
Methods are something like a function, but not a function.
sentence = "These are few wise words!"
sentence.startswith("T") # it returns boolean, True if sentence starts with whatever argument is passed
True
sentence.startswith("N")
False
sentence.endswith("words")
False
sentence.endswith("words!")
True
sentence.endswith("Words!") # it is case sensitive
False
sentence.isupper()
False
"THIS".isupper()
True
"THIS".islower()
False
sentence.isalnum()
False
"this_is_alphanumric".isalnum()
False
"user123".isalnum()
True
"hello".capitalize()
'Hello'
sentence.capitalize()
'These are few wise words!'
sentence.upper()
'THESE ARE FEW WISE WORDS!'
sentence.lower()
'these are few wise words!'
sentence # calling any method will not change original string! because string is immutable object
'These are few wise words!'
sentence.rjust(50)
' These are few wise words!'
sentence.ljust(50)
'These are few wise words! '
sentence.center(50)
' These are few wise words! '
sentence.center(100)
' These are few wise words! '
sentence.replace("wise", "foolish")
'These are few foolish words!'
foolish = sentence.replace("wise", "foolish")
print(sentence)
These are few wise words!
print(foolish)
These are few foolish words!
sentence.split() # will split the string on white spaces into multiple string
['These', 'are', 'few', 'wise', 'words!']
path = "c:\\Prgram Files\\Python"
path.split("\\")
['c:', 'Prgram Files', 'Python']
path = "/home/yoouhome/Documents"
path.split("/")
['', 'home', 'yoouhome', 'Documents']
"_".join(["one","two","three","four"])
'one_two_three_four'
"/".join(['','home','vikrant','Documents'])
'/home/vikrant/Documents'
Questions
line = " this is first line from file "
line.strip() # trailing whitespaces from both ends
'this is first line from file'
line.rstrip()
' this is first line from file'
line.lstrip()
'this is first line from file '
line.strip().split()
['this', 'is', 'first', 'line', 'from', 'file']
line.strip.split()[-1]
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-78-4c6ea2f3f972> in <module> ----> 1 line.strip.split()[-1] AttributeError: 'builtin_function_or_method' object has no attribute 'split'
line.strip().split()[-1] # last word from a sentence
'file'
nums = list(range(1,5))
nums
[1, 2, 3, 4]
nums.index(3)
2
nums.index(4)
3
names = ["Aparna","Arpita","Ana","Alice"]
names.index("Alice")
3
names.count("Ana")
1
ones = [1, 1, 1,1 , 1]
ones.count(1)
5
empty = []
empty.append(1) # it has changes original list
empty
[1]
empty.insert(0, 34)
empty
[34, 1]
empty.insert(1, 45)
empty
[34, 45, 1]
empty.extend([0, 0, 0])
empty
[34, 45, 1, 0, 0, 0]
empty.remove(0) # remove first occurence of 0
empty
[34, 45, 1, 0, 0]
empty.remove(45)
empty
[34, 1, 0, 0]
c = nums.count(1)
c
1
y = empty.insert(1, 10)
print(y)
None
empty
[34, 10, 1, 0, 0]
x = nums.pop() # it will remove last item , but also retun the removed item
nums
[1, 2, 3]
x
4
digits = [0, 1, 2, 3, 4, 5]
digits.clear()
digits
[]
empty.sort() # it will sort inplace, so original list is no more available
empty
[0, 0, 1, 10, 34]
Questions
>>> count_words("this has four words")
4
>>> path = "/home/vikrant/Documents/hello.py"
>>> filename(path)
'hello.py'
def count_words(text):
words = text.split()
return len(words)
def filename(path):
"""returns filename from given path.
assumes unix style of path
"""
tokens = path.split("/")
return tokens[-1]
count_words("this is some big sentence with some words in it")
10
filename("/home/vikrant/hello.txt")
'hello.txt'