About Lesson
Python is known for its simplicity and readability, making it a great programming language for beginners. Here’s a detailed guide to Python syntax with examples.
Key Characteristics of Python Syntax
Whitespace and Indentation:
- Python uses indentation (spaces or tabs) to define code blocks instead of braces
{}
or keywords likebegin
/end
. - Proper indentation is mandatory, and incorrect indentation will result in errors.
- Python uses indentation (spaces or tabs) to define code blocks instead of braces
Case Sensitivity:
- Python is case-sensitive. For example,
variable
andVariable
are treated as distinct identifiers.
- Python is case-sensitive. For example,
Line Structure:
- Each statement typically occupies a single line.
- Use a backslash (
) to continue a statement across multiple lines.
Basic Syntax Elements
1. Comments
- Use the
#
symbol to write comments in Python. Comments are ignored during execution and are used to explain code.
2. Variables and Assignment
- Variables store data. No explicit declaration is required.
3. Data Types
- Python supports several data types:
int
,float
,str
,bool
,list
,tuple
,dict
, andset
.
Indentation
In Python, indentation is used to define blocks of code, such as functions, loops, and conditionals.
- Indentation must be consistent. For example:
- Either use 4 spaces or a single tab per level of indentation.
if 6 > 3:
print("Six is greater than three") # Indented block
Basic Operations
1. Arithmetic Operators
- Perform basic arithmetic like addition, subtraction, multiplication, and division.
2. Comparison Operators
- Compare values (e.g., equal, greater than, less than).
3. Logical Operators
- Perform logical operations like
and
,or
, andnot
.