Page 108 - 2502_Pakistan-kifayat_C-8
P. 108

This conversion helps learners understand the logic behind each block and how it translates into actual
              programming languages like Python or JavaScript. It also builds confidence and prepares students for
              real-world coding, where text-based languages are used more often. By practicing both types, students
              can better understand programming concepts and become stronger problem solvers.

              Variables

              In Scratch, you create a variable and set or change its value using blocks such as,












              Whereas, in Python, you create a variable by simply assigning a value to a variable name.

              The conversion of Scratch blocks to Python code is shown in the following table:

                                  Scratch Code                                        Python Code


                                                                     num = 10




                                                                     num = num + 5


              Loops


              In  Scratch, loops  help  you  repeat actions  multiple  times or until  a certain condition  is  met. When
              converting these loops to Python, you use Python’s for and while loops, which serve the same purpose
              but are written in text form.
              The conversion of Scratch loop blocks to Python is shown in the following table:

                         Scratch Code                                        Python Code

                                                   import time  # to use the sleep function
                                                   for i in range(5):

                                                       print("Hello")
                                                       time.sleep(2)  # pause for 2 seconds




                                                   while True:
                                                       print("Hello")











                  106  Premium Edition-VIII
   103   104   105   106   107   108   109   110   111   112   113