PYTHON 16
CSIT 111 Study 5/06/2025 Guest on 7th May 2025 01:46:04 AM
  1. # printing output
  2. print("Message to the user")
  3. # variables
  4. job = "fisherman"
  5. print(job)
  6. job = "software developer"
  7. number = 1
  8. print(number)
  9. # concatenataion
  10. print(job + " " + str(number))
  11. number += 5
  12. print(number * 50)
  13.  
  14. # input from user
  15. age = int(input("What is your age?\n"))
  16. print(age)
  17. # conditions
  18. print(age >= 18)
  19. # branching/decisions/ "if statements"
  20. if age >= 21:
  21.         print("You can drink")
  22. elif age >= 25:
  23.         print("this is unreachable because 21 > 25")
  24. else:
  25.         print("you can't drink")
  26. # loops (for/while)
  27. # keep adding to a total until user quits
  28. user_input = input("Enter a number or q to quit:\n")
  29. total = 0
  30. while user_input != "q":
  31.         total = total + int(user_input)
  32.         print(total)
  33.         user_input = input("Enter another number or q to quit:\n")
  34.  
  35. name_of_student = "Josh"
  36. print("Length of Josh = " + str(len(name_of_student)))
  37. # range function (start/stop/step)
  38. #we go up to but not including the stop
  39. # 1 parameter = the stop
  40. # J - O - S -H
  41. for i in range(len(name_of_student)):
  42.         print(name_of_student[i])
  43.  
  44. # H - S - O - J
  45.  
  46. # 3 parameters = start and stop and step
  47. for i in range(len(name_of_student) - 1, -1, -1):
  48.         print(name_of_student[i])
  49. # H - S - O - J
  50. # 2 parameters = start and stop
  51. for i in range(0, len(name_of_student)):
  52.         # 4- 1 - 0 = = 3 = H
  53.         # 4- 1 - 1 = 2 = S
  54.         # 4- 1 - 1 = 1 = O
  55.         # 4- 1 - 1 = 0 = J
  56.         print(name_of_student[len(name_of_student) - 1 -i])
  57.  
  58. # functions/methods/procedures/subprograms/subroutines/modules
  59.  
  60. # input/output or even just named with no input or output
  61.  
  62. # data validation
  63.  
  64. # arrays/lists
  65. colors = ["blue", "red", "green", "purple", "yellow"]
  66. for color in colors:
  67.         print(color)
  68.  
  69. def valid_color(my_color):
  70.         for color in colors:
  71.                 if my_color == color:
  72.                         return True
  73.         # if not found
  74.         return False
  75. print(valid_color("fruit")) #False
  76. print(valid_color("blue")) #True
  77.  
  78. # file manipulation

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.