chrisluiz16

Untitled

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