Page 107 - TCS ICT Book 8
P. 107

The City School  2021-2022




             Follow the steps to run python as script on IDLE:

             1.  On Python IDLE click File>>New File.
             2.  In the resulting window type the following program and then save the file as

                test1.py in the default location.
             3.  Then press F5 to execute the program.

             4.  The program output will be 15..


             Sample code:

             # This program adds two numbers

             x = 10                                                                                                 Programming Robots & Single-Board Computers
             y = 5

             # Add two numbers and store result in z
             z = x + y

             # Display the sum
             print(z)



             5.5.  Variables in Python


             A variable is the storage placeholder for text and numbers. It must have a name
             so that you can find it again. The variable is always assigned with an equality sign,

             followed by the value of the variable. Variables can store data that can be used
             elsewhere. They’re one of the most powerful tools programmers can use.

             In python IDLE, enter the following statement: score=9 All this does is tell Python
             that you want to use score as a new name for the value 9. After this point, whenever

             Python sees score, it will insert the value 9. To demonstrate this, try entering the
             following: print(score).

             Remember that python executes instructions in order, and you must give score a
             value before you use it or you will get an error.

             Sample code:

             # This defines a variable score and assigns a value to it

             score = 9
             # Display the value stored in the variable

             print(score)











                                                                                                                107
   102   103   104   105   106   107   108   109   110   111   112