Moving Block To Text
Moving Block To Text
1. Scratch vs Python
Python
print("Hello World")
4. Flowchart
● + : Addition
● - : Subtraction
● * : Multiplication
● / : Division
● // : Floor Division (returns an integer)
● % : Modulus (returns the remainder of division)
● ** : Exponentiation (raises a number to the power of another)
Example:
Python
a = 5
b = 2
print(a + b) # Output: 7
● == : Equal to
● != : Not equal to
● > : Greater than
● < : Less than
● >= : Greater than or equal to
● <= : Less than or equal to
Example:
Python
a = 5
b = 2
print(a > b) # Output: True
7. Python Programming Basics
IDLE
● IDLE (Integrated Development and Learning Environment) is a simple editor for writing
Python programs.
● IDLE converts your Python code to machine language, which the computer can
understand and execute.
Interactive Mode
● In Interactive Mode, you type commands one at a time directly into the Python shell (the
>>> prompt). It is useful for testing small bits of code.
● Example:
Python
>>> print("Hello World")
Hello World
●
Disadvantage: It cannot save your work.
Script Mode
● In Script Mode, you write your program in a text editor and save it with a .py extension.
You can run multiple commands at once.
● Example:
Python
# Save this as my_program.py
print("Hello World")
print("Python is fun!")
● After writing the program in Script Mode and saving it, you can run it using IDLE or the
terminal/command line:
1. Open your Python editor.
2. Write your code and save it as my_program.py.
3. In IDLE, click Run > Run Module or press F5.
4. If using terminal, type:
Unset
python my_program.py
Python
print("This is beautiful")
●
help(): Provides information about a function or module.
Python
help(print)
●
exit(): Exits the Python shell.
Python
exit()
●
Calculation without print: You can perform calculations without using the print()
function in the Interactive mode, but you need print() to display the result.
● If your code has an error, everything may show up in red when you run the program.
● Common errors include missing parentheses, incorrect syntax, or using a wrong name.
● Correction can be done in the text editor where you write the code.
● You can add comments in your Python code using the # symbol. Comments are
ignored by Python and are used to explain the code for others or for yourself.
Python
# This is a comment
print("Hello World") # This prints Hello World
11. Variables
Python
name = "John"
age = 12
●
User Input: To take input from the user, use the input() function:
Python
name = input("Enter your name: ")
print("Hello, " + name)
● String: Text data. Default type for any data enclosed in quotes.
Python
name = "John"
●
Integer: Whole numbers. Use int() to convert.
Python
age = int(input("Enter your age: "))
●
Float: Decimal numbers. Use float() to convert.
Python
price = float(input("Enter price: "))
Casting
Python
age = int("25") # String "25" is cast to an integer
13. Flowchart and Algorithm
Python
age = 12
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Use debugging techniques like reading error messages carefully and checking each line of code
to find and fix the error.