chrisluiz16

Day3 python

Jul 19th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.03 KB | None | 0 0
  1. ##------------------------------------------ Task 1 -----------------------------------------------##
  2.  
  3.  
  4. ## String object
  5.  
  6. name = "Prince"
  7. print(len(name))
  8. print(name[0])
  9. print(name[-1])
  10.  
  11. port = 2020
  12. print(str(port)[-1])
  13.  
  14. ## Slice operation
  15.  
  16. timestamp = "19-07-2023"
  17. day = timestamp[0:2]
  18. print(day)
  19.  
  20. print(timestamp[3:5])
  21.  
  22. year = timestamp[-4:]
  23. print(year)
  24.  
  25. vehicle_no = "KL07-CA-3254"
  26. print(vehicle_no[8:])
  27.  
  28. print(vehicle_no[-4:])
  29.  
  30. ## For loop
  31.  
  32. name = "prince"
  33. print("Program starts here")
  34. for item in name:
  35.     print(item)
  36. print("Program ends here")
  37.  
  38. lang = "malayalam"
  39. for char in lang:
  40.   if char == "a":
  41.     print(char)
  42.  
  43. lang = "malayalam"
  44. x = 0
  45. for item in lang:
  46.   if item == "a":
  47.     x = x + 1
  48. print(x)
  49.  
  50. ## Buil in functions
  51.  
  52. ### lowerfn()
  53.  
  54. name = "PrinCE"
  55. print("The name", name , "is converted to lower :", name.lower())
  56.  
  57. ### upper
  58.  
  59. name = "prince"
  60. print("The name", name , "is converted to upper :", name.upper())
  61.  
  62. ### isdigit()
  63.  
  64. s1 = "abc"
  65. s2 = "abc123"
  66. s3 = "123"
  67.  
  68. print(s1, s1.isdigit())
  69. print(s2, s2.isdigit())
  70. print(s3,s3.isdigit())
  71.  
  72. num = input("Enter the digit to check whether it's odd or even : ")
  73. if num.isdigit():
  74.   if int(num) % 2 == 0:
  75.     print("The number is even")
  76.   else:
  77.     print("The number is odd")
  78. else:
  79.   print("Sorry invalid digit")
  80.  
  81. ### isalpha
  82.  
  83. s1 = "abc123"
  84. s2 = "123"
  85. s3 = "abc"
  86.  
  87. print(s1, s1.isalpha())
  88. print(s2,s2.isalpha())
  89. print(s3,s3.isalpha())
  90.  
  91. word = input("Enter the word to check : ")
  92. if word.isalpha():
  93.   print("The word you entered is an alphabet")
  94. if word.isdigit():
  95.   print("The input you entered is a digit")
  96.  
  97. word = input("Enter a character : ")
  98. if len(word) == 1:
  99.   if word.isalpha():
  100.     print("Your input is an alphabet")
  101.   if word.isdigit():
  102.     print("Your input is a digit")
  103. else:
  104.   print("Please enter one char only....try again, Thank you!")
  105.  
  106. ### replace
  107.  
  108. word = "malayalam"
  109. new = word.replace("a","A")
  110. print(word, "is now converted to upper A", new)
  111.  
  112. new = word.replace("a"," ")
  113. print(new)
  114.  
  115.  
  116. new = word.replace("a","")
  117. print(new)
  118.  
  119. ### count
  120.  
  121. word = "Malayalam"
  122. new = word.count("a")
  123. new1 = word.count("l")
  124. new2 = word.count("m")
  125.  
  126. print(new , new1, new2)
  127.  
  128. word = "malayaLAm"
  129. x = 0
  130. y = 0
  131. for item in word:
  132.   if item == "a":
  133.     x = x + 1
  134.   if item == "A":
  135.     y = y + 1
  136. print("The number of a in the word", word , x)
  137. print("The number of A in the word", word , y)
  138.  
  139.  
  140. ### Starts and endswith
  141.  
  142. conf = "/etc/httpf/conf/httpd.conf"
  143. result = conf.startswith("/")
  144. print(result)
  145.  
  146.  
  147. result = conf.startswith("/etc")
  148. print(result)
  149.  
  150. result = conf.startswith("etc")
  151. print(result)
  152.  
  153. result = conf.endswith(".conf")
  154. print(result)
  155.  
  156. result = conf.endswith("httpd.conf")
  157. print(result)
  158.  
  159. result = conf.endswith(".cfg")
  160. print(result)
  161.  
  162. ### is upper and is lower
  163.  
  164. s1 = "a"
  165. s2 = "AA"
  166. s3 = "ABCabc"
  167. s4 = "a12"
  168. s5 = "A"
  169.  
  170. print(s1, s1.isupper())
  171. print(s1,s1.islower())
  172.  
  173. print(s2,s2.isupper())
  174. print(s2, s2.islower())
  175.  
  176. print(s3,s3.isupper())
  177. print(s3, s3.islower())
  178.  
  179. print(s4,s4.isupper())
  180. print(s4, s4.islower())
  181.  
  182. print(s5,s5.isupper())
  183. print(s5, s5.islower())
  184.  
  185. ##------------------------------------------ Task 2 -----------------------------------------------##
  186.  
  187. ## 1] Replace character with 23-12-2022 to 23/12/2022
  188.  
  189. word = "23-12-2022"
  190. new = word.replace("-","/")
  191. print(new)
  192.  
  193. Ans] 23/12/2022
  194.  
  195. ### 2] Find the n.o of occurence of the char "a" or "A "
  196.  
  197. word = "MAlayaLAm"
  198. x = 0
  199. y = 0
  200. for item in word:
  201.   if item == "a":
  202.     x = x + 1
  203.   if item == "A":
  204.     y = y + 1
  205. print("The number of a in the word", word,"is : ", x)
  206. print("The number of A in the word", word,"is : ", y)
  207.  
  208. Ans]
  209. The number of a in the word MAlayaLAm is :  2
  210. The number of A in the word MAlayaLAm is :  2
  211.  
  212.  
  213. ### 3] Print the total n.o of digits.
  214.  
  215. word = "a1b2c3D4E4F5"
  216. x = 0
  217. for item in word:
  218.   if item.isdigit():
  219.     x = x + 1
  220. print("The total n.o of digits in the word", word, "is: ", x)
  221.  
  222. Ans] The total n.o of digits in the word a1b2c3D4E4F5 is:  6
  223.  
  224.  
  225. ### 4] Print the total n.o of small letters
  226.  
  227. word = "a1b2c3D4E4F5"
  228. x = 0
  229. for item in word:
  230.   if item.islower():
  231.     x = x + 1
  232. print("The total n.o of small letters in the word", word,"is: ", x )
  233.  
  234. Ans] The total n.o of small letters in the word a1b2c3D4E4F5 is:  3
  235.  
  236.  
  237. ### 5] Print the total n.o of Capital letters
  238.  
  239. word = "a1b2c3D4E4F5"
  240. x = 0
  241. for item in word:
  242.   if item.isupper():
  243.     x = x + 1
  244. print("The total n.o of Capital letters in the word", word,"is: ", x )
  245.  
  246. Ans] The total n.o of Capital letters in the word a1b2c3D4E4F5 is:  3
  247.  
  248.  
  249. ### 6] Find the number of spaces
  250.  
  251. sentence = "This post is to illustrate how to access the docker container network namespace"
  252. x = 0
  253. for item in sentence:
  254.   if item == " ":
  255.     x = x + 1
  256. print("the number of spaces is: ",x)
  257.  
  258. Ans] the number of spaces is:  12
  259.  
  260. ### 7] Fix the error
  261.  
  262. #### Corrected code
  263.  
  264. number = input("enter a number ")
  265. if number.isdigit():
  266.   if int(number) % 2 == 0:
  267.     print("even")
  268.   else:
  269.     print("odd")
  270. else:
  271.   print("Sorry you've entered a wrong input")
  272.  
  273.  
Advertisement
Add Comment
Please, Sign In to add comment