chrisluiz16

day 4 python

Jul 20th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. ## Occurence ot toal A's in word
  2.  
  3. word = "malayAlam"
  4. c = 0
  5. for item in word:
  6.   if item in "aA":
  7.     c = c + 1
  8. print(c)
  9.  
  10. ## List datatype
  11.  
  12. names = ['chris','prince','vysakh']
  13. print(names[0])
  14. print(names[1])
  15. print(names[-1])
  16.  
  17. names = ['chris','prince','vysakh']
  18. for item in names:
  19.   print(item)
  20.  
  21. r = len(names)
  22. print(r)
  23.  
  24. ## Adding new item
  25.  
  26.  
  27.  
  28. names = ['chris','prince','vysakh']
  29. print('before : ', names)
  30. names.append('sam')
  31. names.append('john')
  32. names.append('sam')
  33. names.append('kevnin')
  34. names.append(10)
  35. names.append(55.56)
  36. print('after', names)
  37.  
  38. names = ['chris','prince','sam','kevin','sam']
  39. r = 'nidhin' not in names
  40. print(r)
  41.  
  42. names = ['chris', 'prince', 'vysakh', 'sam', 'john', 'sam', 'kevin', 10, 55.56 ]
  43. r = names.count('sam')
  44. print(r)
  45.  
  46. ## Replace
  47.  
  48. names = ['chris','prince','nidhin']
  49. names[0] = 'vysakh'
  50. print(names)
  51.  
  52. ## Empty list
  53.  
  54. persons = []
  55.  
  56. persons.append('sam')
  57. persons.append('john')
  58. persons.append('kevin')
  59. print(persons)
  60.  
  61. ## Extend
  62.  
  63. mylist = []
  64. l1 = ['a','b']
  65. l2 = ['c','d','e']
  66. l3 = ['f','g']
  67.  
  68. mylist.extend(l1)
  69. print(mylist)
  70.  
  71. mylist.extend(l2)
  72. print(mylist)
  73.  
  74. mylist.extend(l3)
  75. print(mylist)
  76.  
  77.  
  78.  
  79. ## String in list
  80.  
  81. s = "Think about an OS like a car engine. An engine that can run its own"
  82. words = s.split()
  83. for word in words:
  84.   print(word)
  85.  
  86. r = s.split()[-1]
  87. print(r)
  88.  
  89. ## Long sentence
  90.  
  91. s = """Linux began in 1991 as a personal project by Finnish student Linus Torvalds: to create a new free operating system kernel .
  92. The resulting Linux kernel has mykernel been marked by constant growth throughout its history."""
  93.  
  94. word = s.split()
  95. print(word)
  96.  
  97. word = s.lower().split().count('kernel')
  98. print(word)
  99.  
  100. s = """Linux began in 1991 as a personal project by Finnish student Linus Torvalds: to create a new free operating system kernel .
  101. The resulting Linux kernel has mykernel been marked by constant growth throughout its history."""
  102.  
  103. words = s.lower().split()
  104. c = 0
  105. for word in words:
  106.   if word == 'kernel':
  107.     c = c + 1
  108. print(c)    
  109.  
  110. # Task 2
  111.  
  112. ## 1] find the sum of digits.
  113.  
  114. s = 'a1b2c3D4E4F5'
  115. y = 0
  116. for item in s:
  117.   if item.isdigit():
  118.     x = int(item)
  119.     y = x + y
  120. print("The sum of digits in the word is:", y)
  121.  
  122.  
  123. ## 2] print odd  digits.
  124.  
  125. s = '13245679134'
  126. for odd in s:
  127.   if int(odd) % 2 != 0:
  128.     print(odd)
  129.    
  130.  
  131. ## 3]  Print even  digits.
  132.  
  133. s = '13245679134'
  134. for even in s:
  135.   if int(even) % 2 == 0:
  136.     print(even)
  137.    
  138.  
  139. ## 4]  Print the words.
  140.  
  141. mylist = [ "sam" , "priyesh" , "1" , 2 , 3 , 4  , "prem" ]
  142. for word in mylist:
  143.     x = str(word)
  144.     if x.isalpha():
  145.       print(x)
  146.  
  147. ## 5] Print the digits
  148.  
  149. mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4 , "prem" ]
  150. for digits in mylist:
  151.   x = str(digits)
  152.   if x.isdigit():
  153.     print(x)
  154.  
  155. ## 6] Find the sum of numbers.
  156.  
  157.  
  158. mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4, "prem" ]
  159. z = 0
  160. for num in mylist:
  161.   x = str(num)
  162.   if x.isdigit():
  163.     y = int(x)
  164.     z = y + z
  165. print(z)
  166.  
  167.  
  168. ## 7] Print the even numbers.
  169.  
  170. mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4 , "prem" ]
  171. for even in mylist:
  172.   x = str(even)
  173.   if x.isdigit():
  174.     if int(x) % 2 == 0:
  175.       print(x)
  176.  
  177. ## 8] Add numbers to nlist.
  178.  
  179. nlist = []
  180. mylist = [ "sam" , "priyesh" , "1" , "2" , 3 , 4  , "prem" ]
  181. for item in mylist:
  182.   x = str(item)
  183.   if x.isdigit():
  184.     y = int(x)
  185.     nlist.append(y)
  186. print(nlist)
  187.  
  188. ## 9] find the word starts with "k".
  189.  
  190. s = """The OS includes some common core components, like the GNU tools,
  191. among others. These kick tools give the user a way to manage the resources
  192. provided by the kernel, Kernel install additional  knee software, configure performance
  193. and security Knife settings, and more. keep All of know these tools bundled together
  194. make up the knee  keen functional kick operating system.  Because Linux is an open source
  195. OS, combinations of kick software can vary between Linux distributions."""
  196.  
  197. s = s.replace(",","")
  198. s = s.replace(".","")
  199. s = s.lower().split()
  200. for item in s:
  201.     if item[0] == "k":
  202.       print(item)
  203.  
  204.  
  205. ## 10] add words starts with "k" to klist.
  206.  
  207. klist = []
  208. s = """The OS includes some common core components, like the GNU tools,
  209. among others. These kick tools give the user a way to manage the resources
  210. provided by the kernel, Kernel install additional  knee software, configure performance
  211. and security Knife settings, and more. keep All of know these tools bundled together
  212. make up the knee  keen functional kick operating system.  Because Linux is an open source
  213. OS, combinations of kick software can vary between Linux distributions."""
  214. s = s.replace(",","")
  215. s = s.replace(".","")
  216. s = s.lower().split()
  217. for item in s:
  218.     if item[0] == "k":
  219.       klist.append(item)
  220. print(klist)
  221.  
  222. ## 11] add words starts with "k" to klist, avoid duplicate.
  223.  
  224. klist = []
  225. s = """The OS includes some common core components, like the GNU tools,
  226. among others. These kick tools give the user a way to manage the resources
  227. provided by the kernel, Kernel install additional  knee software, configure performance
  228. and security Knife settings, and more. keep All of know these tools bundled together
  229. make up the knee  keen functional kick operating system.  Because Linux is an open source
  230. OS, combinations of kick software can vary between Linux distributions."""
  231. s = s.replace(",","")
  232. s = s.replace(".","")
  233. s = s.lower().split()
  234. for item in s:
  235.     if item.startswith("k"):
  236.       if item not in klist:
  237.         klist.append(item)
  238. print(klist)
  239.  
  240.  
  241.  
  242.  
Advertisement
Add Comment
Please, Sign In to add comment