-
Hajipur, Bihar, 844101
Syntax refers to the structure and set of rules that define how Python code is written and interpreted.
Python is known for its simple, clean, and readable syntax, which makes it one of the most beginner-friendly programming languages.
Let’s explore the basic rules of Python syntax.
To print text or values in Python:
print("Hello, World!")
Python uses indentation (whitespace) to define blocks of code.
There are no curly braces {}
like other languages.
if 5 > 2:
print("5 is greater than 2") # This line is indented
Incorrect indentation will raise an error.
Use #
to write single-line comments:
# This is a comment
print("Hello")
No need to declare data types:
x = 10
name = "John"
Python is case-sensitive:
a = 10
A = 20
print(a) # Outputs 10
print(A) # Outputs 20
No need to add a semicolon ;
at the end, but it’s allowed:
print("Hello") # Correct
print("Hi");
Separate them using a semicolon:
x = 5; y = 10; print(x + y)
Use \
or parentheses:
total = 1 + 2 + \
3 + 4
Or:
total = (1 + 2 +
3 + 4)
Concept | Example |
---|---|
print("Hello") |
|
Indentation | if x > 0:\n print(x) |
Variable | x = 5 |
Case-sensitive | a ≠ A |
Comment | # This is a comment |
Q1. Write a Python program that prints your name.
Q2. Write a Python program to assign the number 50 to a variable and print its value.
Q3. Write a Python program using indentation with an if
statement to print "Yes"
if a condition is true.
Q4. Create a variable name
and print a greeting using it (e.g., “Hello, John”).
Q5. Write a Python program that includes a comment above a print statement.
Q6. Use a backslash (\
) to break a long line of code into two lines and print a result.
Q7. Assign a float and a string to two different variables and print both.
Q8. Write a Python program to print the sum of two numbers.
Q9. Use one line to declare two variables with values and print both.
Q10. Write a program to show that Python is case-sensitive by creating two variables: a
and A
, and print both.
Q1: What is the output of the print function when given a string "Hello"?
Q2: What is the main purpose of indentation in Python?
Q3: Which symbol is used to add comments in Python?
Q4: Python treats variable names with different cases as:
Q5: Which of the following is a valid Python print statement?
Q6: What causes a syntax error in Python?
Q7: Python source files should be saved with which extension?
Q8: What does the print() function do?
Q9: Which of the following is true about Python?
Q10: Which of the following is allowed in Python syntax?