Structure of a Python Program
- Script Mode vs. Interactive Mode: Python can be run in script mode (writing code in a file and executing it) or interactive mode (typing code directly into the Python interpreter).
- Indentation: Python uses indentation to define blocks of code. Consistent indentation is crucial as it replaces the use of braces
{}found in other programming languages. - Comments: Use
#for single-line comments and'''or"""for multi-line comments. Comments are ignored by the interpreter and are used to explain code.
Data Types, Variables, and Operators
- Data Types: Python supports various data types including:
- Integers (
int): Whole numbers, e.g.,5,-3. - Floating-point (
float): Decimal numbers, e.g.,3.14,-0.001. - Strings (
str): Sequence of characters, e.g.,"Hello, World!". - Booleans (
bool): RepresentsTrueorFalse.
- Integers (
- Variables: Used to store data values. Variable names should be descriptive and follow naming conventions (e.g.,
my_variable).
pythonage = 25 name = "Alice" is_student = True - Operators: Used to perform operations on variables and values.
- Arithmetic Operators:
+,-,*,/,%,**,//. - Relational Operators: ==, !=
,>,<,>=,<=`. - Logical Operators:
and,or,not.
- Arithmetic Operators: