chrisluiz16

python day 2

Jul 18th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1.  
  2.  
  3. ##------------------------------------------ Task 1 --------------------------------##
  4.  
  5. ## Simple Calculator
  6.  
  7. x = 10
  8. y = 20
  9. total = x + y
  10. print(total, type(total))
  11.  
  12. x = 10
  13. y = 20
  14. total = x - y
  15. print(total, type(total))
  16.  
  17. x = 10
  18. y = 20
  19. total = y / x
  20. print(total, type(total))
  21.  
  22. x = 10
  23. y = 20
  24. total = y % x
  25. print(total)
  26.  
  27. ## Type Conversion
  28.  
  29. x = 10
  30. y = 5
  31.  
  32. print( float(x) / float(y) )
  33.  
  34. x = 100
  35. y = 5
  36. x_str = str(x)
  37. y_str = str(y)
  38. total = x_str + y_str
  39. print(total, type(total))
  40.  
  41. x = "10"
  42. y = "5"
  43. x_int = int(x)
  44. y_int = int(y)
  45. total = x_int + y_int
  46. print(total,type(total))
  47.  
  48. x = 99.8
  49. x_int = int(x)
  50. print(x_int, type(x_int))
  51.  
  52. x = 5
  53. y = "5.2"
  54. z = 3
  55.  
  56. x_float = float(x)
  57. y_float = float(y)
  58. z_float = float(z)
  59.  
  60. print(x_float, type(x_float))
  61. print(y_float, type(y_float))
  62. print(z_float, type(z_float))
  63.  
  64. ## Input function
  65.  
  66. name = input("Enter your name : ")
  67.  
  68. print("Good Morning", name)
  69.  
  70. ## Calculator with input fn()
  71.  
  72. first = input("Enter your first number : ")
  73. second = input("Enter your second number : ")
  74. third = input("What you want to do add/substract/divide : ")
  75.  
  76. if third == "+":
  77.   add = int(first) + int(second)
  78.   print(add)
  79. if third == "-":
  80.   sub = int(first) - int(second)
  81.   print(sub)
  82. if third == "/":
  83.   div = int(first) / int(second)
  84.   print(div)
  85.  
  86. ## Comparison operators
  87.  
  88. x = 10
  89. y = 5
  90.  
  91. r = x > y
  92. print("x > y", r)
  93.  
  94. r = x < y
  95. print("x < y", r)
  96.  
  97. r = x >= 20
  98. print("x >=20 ", r)
  99.  
  100. r = x <= 10
  101. print( "x <= 10", r)
  102.  
  103. r = x == y
  104. print("x == y", r)
  105.  
  106. r = x == 10
  107. print("x == 10", r)
  108.  
  109. r = x != 10
  110. print("x != 10", r)
  111.  
  112. r = y != 10
  113. print("y != 10", r)
  114.  
  115. ## String comparison
  116.  
  117. str1 = "abc123"
  118. str2 = "abc123"
  119. str3 = "abc"
  120.  
  121. result = str1 == str2
  122. print(result)
  123.  
  124. result = str1 != str2
  125. print(result)
  126.  
  127. result = str3 == str2
  128. print(result)
  129.  
  130. ## Len function
  131.  
  132. name1 = "prince"
  133. name2 = "chris"
  134. name3 = "Vysakh UT"
  135.  
  136. result = len(name1)
  137. print(result, type(result))
  138.  
  139. result = len(name2)
  140. print(result, type(result))
  141.  
  142. result= len(name3)
  143. print(result, type(result))
  144.  
  145. port = 8080
  146. result = len(str(port))
  147. print(result, type(result))
  148.  
  149.  
  150. ## Membership Operation
  151.  
  152. fruit = "Pineapple"
  153. result = "apple" in fruit
  154. print(result)
  155.  
  156. result = "in" in fruit
  157. print(result)
  158.  
  159. result = "p" in fruit
  160. print(result)
  161.  
  162. result = "P" not in fruit
  163. print(result)
  164.  
  165. ## Subscript Operation
  166.  
  167. name = "Richard"
  168. r = name[0]
  169. print(r)
  170.  
  171. r = name[3]
  172. print(r)
  173.  
  174. ## If else condition
  175.  
  176. num = input("Enter a number : ")
  177. x_int = int(num)
  178.  
  179. if x_int >= 10:
  180.   print("The number is greater than 10")
  181. else:
  182.   print("Sorry the number is less than 10")
  183.  
  184.  
  185. ##------------------------------------------ Task 2 --------------------------------##
  186.  
  187. ## 1] Print the length of the string
  188.  
  189. word = input("Enter the word so as to count the n.o of characters : ")
  190. print( "The length is : ", len(word))
  191.  
  192. Ans] Enter the word so as to count the n.o of characters : Kerala
  193. The length is :  6
  194.  
  195. ## 2] Check the substring is present
  196.  
  197. word = input("Enter the word : ")
  198. sub = input("Which word to check : ")
  199. if sub in word:
  200.   print("Yes there is", sub , "in the word", word)
  201. else:
  202.   print("Sorry no match found")
  203.  
  204. Ans ] Enter the word : Pineapple
  205. Which word to check : apple
  206. Yes there is apple in the word Pineapple
  207.  
  208. ## 3] Check the number is odd or even
  209.  
  210. num = input("Enter the number to check whether it's odd or even : ")
  211. x_num = int(num)
  212. if x_num % 2 == 0:
  213.   print( "The n.o", x_num , "is even")
  214. else:
  215.   print( "The n.o", x_num , "is odd")
  216.  
  217. Ans] Enter the number to check whether it's odd or even : 5
  218. The n.o 5 is odd
  219.  
  220. ## 4] check the word that starts with "a" or "A"
  221.  
  222. word = input("Enter the word : ")
  223. check = word[0]
  224. if check == "a" or check == "A":
  225.  print("okay")
  226. else:
  227.  print("No")
  228.  
  229. Ans] Enter the word : Apple
  230. okay
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment