Python is a high-level, interpreted, and general-purpose programming language known for its simplicity and readability.
- It supports multiple programming paradigms such as procedural, object-oriented, and functional programming.
Python Syntax:
Python syntax refers to the set of rules that defines how Python programs are written and interpreted.
→ Key Features of Python Syntax:
1.) Case Sensitivity: Python is case-sensitive:
myVar = 10
MyVar = 20 # Different variables
2.) No Semicolons: Statements do not require a semicolon unless multiple statements are on the same line:
print("Hello") # Valid
print("Hello"); print("World") # Also valid
3.) No Braces: Python does not use {}
for code blocks, making code clean and readable.
Basic Syntax Rules:
1.) Printing Output: Use the print() function to display output:
print("Hello, World!")
2.) Taking Input: Use the input() function to get user input
name = input("Enter your name: ")
print("Hello, " + name)
3.) Variables: Variables are dynamically typed (no need to declare data type explicitly)
x = 10 # Integer
y = "Python" # String
z = 3.14 # Float
Comments in Python:
Comments are used to explain the code and make it more readable and are not executed by the python engine. They help make the code more readable and maintainable.
Types of Comments:
- Single-line Comments: Use #
- Multi-line Comments: Enclose comments within “””…..”””
# This is a single line comment
"""
This is a
multi-line comment
"""
Variables in Python:
Variables in Python are used to store data values. Unlike some other programming languages, you don’t need to declare the type of a variable explicitly; Python dynamically determines the type based on the value assigned to it.
Rules for Variable Declaration in Python:
- Variables must start with a letter (a-z, A-Z) or an underscore (_).
- Variable names cannot start with a number.
- Variable names can only contain letters, numbers, and underscores.
- Variable names are case-sensitive.
- Variable names cannot be Python reserved keywords (e.g., if, while, for).
#Rule 1
_variable = "Valid"
name = "Python"
#Rule 2
1name = "Invalid" # SyntaxError
#Rule 3
my_variable1 = 10 # Valid
my-variable = 20 # Invalid (SyntaxError)
#Rule 4
name = "Alice"
Name = "Bob"
print(name) # Outputs: Alice
print(Name) # Outputs: Bob
#Rule 5
if = 10 # SyntaxError
Constants in Python:
Constants in Python are variables whose value should not change during the program’s execution. Unlike some other programming languages, Python does not have built-in support for true constants.
Defining Constants:
1.) Using Naming Convention: Simply use an uppercase variable name:
PI = 3.14159
GRAVITY = 9.8
MAX_USERS = 100
Indentation in Python
Indentation in Python refers to the spaces at the beginning of a line that define the structure of the code. Unlike many other programming languages that use braces {} to define blocks of code, Python uses indentation to signify a block of code.
Key Points:
- Indentation is mandatory in Python.
- All lines in the same block must have the same level of indentation.
- Improper indentation leads to IndentationError.
Example:
# Correct Indentation
if True:
print("This is indented") # Inside if block
print("Still inside if block")
print("Outside if block") # Not indented, outside if block
# Incorrect Indentation
if True:
print("This will cause an error") # IndentationError