Contents
Roadmap info from roadmap website
Basic Syntax
Setup the environment for python and get started with the basics.
Visit the following resources to learn more:
- @article@W3Schools - Python
- @video@Python for Beginners - Learn Python in 1 Hour
- @article@Python Basics
- @article@Learn X in Y Minutes / Python
Characteristics of Python syntax
1. Whitespace and Indentation
-
Significant Whitespace: Python uses indentation to define code blocks, unlike many other languages that use braces
{}or keywords. - Consistent Indentation: All lines in a block must be indented the same amount.
2. Dynamic Typing
- No Need for Explicit Type Declaration: Variables do not require an explicit declaration of data type. The type is inferred at runtime.
- Type Flexibility: Variables can be reassigned to different types.
3. Simple and Clean Syntax
- No Semicolons: Statements end with a newline rather than a semicolon.
- Minimal Boilerplate: Python code tends to be concise and requires less boilerplate compared to other languages.
4. Use of Colons
-
Colon for Code Blocks: Colons
:are used to start a new block of code (e.g., afterif,for,while,def,class).
5. Readable Function Definitions
-
defKeyword: Functions are defined using thedefkeyword followed by the function name and parameters in parentheses. - Optional Annotations: Function parameters and return types can optionally have type annotations, but they are not enforced.
6. Flexible and Intuitive Looping
-
forLoops Over Iterables: Pythonβsforloop is designed to iterate directly over items of any sequence (like lists, strings, or ranges). -
elseClause in Loops:forandwhileloops can have anelseblock that executes if the loop completes normally (not interrupted bybreak).
7. Built-in Data Structures
- Native Support for Lists, Tuples, Dictionaries, and Sets: Python has a wide range of built-in data structures that are easy to use.
- List Comprehensions: Python supports a concise syntax for creating lists based on existing lists.
8. First-Class Functions
- Functions as Objects: Functions in Python are first-class objects, meaning they can be passed as arguments to other functions, returned from functions, and assigned to variables.
9. Lambda Functions
-
Anonymous Functions: Python supports the creation of small anonymous functions using the
lambdakeyword.
10. Exception Handling
-
try-exceptBlocks: Python usestry-exceptblocks for exception handling, with an optionalelseclause for code that runs if no exceptions occur, and afinallyclause for code that runs no matter what.
11. Concise Input and Output
-
printFunction: Python uses a simpleprint()function for output. -
inputFunction: Input from the user is handled with theinput()function.
12. Modules and Imports
-
Simple Import Syntax: Modules are imported using the
importstatement, and you can also import specific functions or variables usingfrom module import name. -
Alias Support: You can alias imported modules or components using
as.
13. Pythonic Idioms
-
withStatement for Resource Management: Thewithstatement is used for resource management, such as opening files. It ensures proper acquisition and release of resources. -
inOperator for Membership Testing: Theinkeyword is used to check membership in sequences like lists, strings, or dictionaries.
14. Rich Standard Library
- Batteries Included: Python comes with a rich standard library that includes modules and functions for handling a wide variety of tasks, making it easy to implement complex functionality with minimal code.