Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Occurence ot toal A's in word
- word = "malayAlam"
- c = 0
- for item in word:
- if item in "aA":
- c = c + 1
- print(c)
- ## List datatype
- names = ['chris','prince','vysakh']
- print(names[0])
- print(names[1])
- print(names[-1])
- names = ['chris','prince','vysakh']
- for item in names:
- print(item)
- r = len(names)
- print(r)
- ## Adding new item
- names = ['chris','prince','vysakh']
- print('before : ', names)
- names.append('sam')
- names.append('john')
- names.append('sam')
- names.append('kevnin')
- names.append(10)
- names.append(55.56)
- print('after', names)
- names = ['chris','prince','sam','kevin','sam']
- r = 'nidhin' not in names
- print(r)
- names = ['chris', 'prince', 'vysakh', 'sam', 'john', 'sam', 'kevin', 10, 55.56 ]
- r = names.count('sam')
- print(r)
- ## Replace
- names = ['chris','prince','nidhin']
- names[0] = 'vysakh'
- print(names)
- ## Empty list
- persons = []
- persons.append('sam')
- persons.append('john')
- persons.append('kevin')
- print(persons)
- ## Extend
- mylist = []
- l1 = ['a','b']
- l2 = ['c','d','e']
- l3 = ['f','g']
- mylist.extend(l1)
- print(mylist)
- mylist.extend(l2)
- print(mylist)
- mylist.extend(l3)
- print(mylist)
- ## String in list
- s = "Think about an OS like a car engine. An engine that can run its own"
- words = s.split()
- for word in words:
- print(word)
- r = s.split()[-1]
- print(r)
- ## Long sentence
- s = """Linux began in 1991 as a personal project by Finnish student Linus Torvalds: to create a new free operating system kernel .
- The resulting Linux kernel has mykernel been marked by constant growth throughout its history."""
- word = s.split()
- print(word)
- word = s.lower().split().count('kernel')
- print(word)
- s = """Linux began in 1991 as a personal project by Finnish student Linus Torvalds: to create a new free operating system kernel .
- The resulting Linux kernel has mykernel been marked by constant growth throughout its history."""
- words = s.lower().split()
- c = 0
- for word in words:
- if word == 'kernel':
- c = c + 1
- print(c)
- # Task 2
- ## 1] find the sum of digits.
- s = 'a1b2c3D4E4F5'
- y = 0
- for item in s:
- if item.isdigit():
- x = int(item)
- y = x + y
- print("The sum of digits in the word is:", y)
- ## 2] print odd digits.
- s = '13245679134'
- for odd in s:
- if int(odd) % 2 != 0:
- print(odd)
- ## 3] Print even digits.
- s = '13245679134'
- for even in s:
- if int(even) % 2 == 0:
- print(even)
- ## 4] Print the words.
- mylist = [ "sam" , "priyesh" , "1" , 2 , 3 , 4 , "prem" ]
- for word in mylist:
- x = str(word)
- if x.isalpha():
- print(x)
- ## 5] Print the digits
- mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4 , "prem" ]
- for digits in mylist:
- x = str(digits)
- if x.isdigit():
- print(x)
- ## 6] Find the sum of numbers.
- mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4, "prem" ]
- z = 0
- for num in mylist:
- x = str(num)
- if x.isdigit():
- y = int(x)
- z = y + z
- print(z)
- ## 7] Print the even numbers.
- mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4 , "prem" ]
- for even in mylist:
- x = str(even)
- if x.isdigit():
- if int(x) % 2 == 0:
- print(x)
- ## 8] Add numbers to nlist.
- nlist = []
- mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4 , "prem" ]
- for item in mylist:
- x = str(item)
- if x.isdigit():
- y = int(x)
- nlist.append(y)
- print(nlist)
- ## 9] find the word starts with "k".
- s = """The OS includes some common core components, like the GNU tools,
- among others. These kick tools give the user a way to manage the resources
- provided by the kernel, Kernel install additional knee software, configure performance
- and security Knife settings, and more. keep All of know these tools bundled together
- make up the knee keen functional kick operating system. Because Linux is an open source
- OS, combinations of kick software can vary between Linux distributions."""
- s = s.replace(",","")
- s = s.replace(".","")
- s = s.lower().split()
- for item in s:
- if item[0] == "k":
- print(item)
- ## 10] add words starts with "k" to klist.
- klist = []
- s = """The OS includes some common core components, like the GNU tools,
- among others. These kick tools give the user a way to manage the resources
- provided by the kernel, Kernel install additional knee software, configure performance
- and security Knife settings, and more. keep All of know these tools bundled together
- make up the knee keen functional kick operating system. Because Linux is an open source
- OS, combinations of kick software can vary between Linux distributions."""
- s = s.replace(",","")
- s = s.replace(".","")
- s = s.lower().split()
- for item in s:
- if item[0] == "k":
- klist.append(item)
- print(klist)
- ## 11] add words starts with "k" to klist, avoid duplicate.
- klist = []
- s = """The OS includes some common core components, like the GNU tools,
- among others. These kick tools give the user a way to manage the resources
- provided by the kernel, Kernel install additional knee software, configure performance
- and security Knife settings, and more. keep All of know these tools bundled together
- make up the knee keen functional kick operating system. Because Linux is an open source
- OS, combinations of kick software can vary between Linux distributions."""
- s = s.replace(",","")
- s = s.replace(".","")
- s = s.lower().split()
- for item in s:
- if item.startswith("k"):
- if item not in klist:
- klist.append(item)
- print(klist)
Advertisement
Add Comment
Please, Sign In to add comment