Python Syntax

Welcome back, brave explorer of the digital realms! Now that we're well-versed in Python and our IDE is all set, let's dive right into the heart of Python - its syntax.

In the magical world of Python, syntax is like the spells we cast - the incantations that make things happen. So let's learn some spells, shall we?

Running a Python script

Running a Python script is like casting your first spell. You write your code in a file ending with .py, and then use Python (the interpreter) to execute the script. You can do this right inside VS Code by right-clicking on your Python file and selecting Run Python File in Terminal. Behold as your script comes alive and displays its output in the terminal!

print("I'm a Python wizard!")

Python identifiers, keywords, and indentation

Identifiers are the names we give to our spells (variables, functions, etc.). They can be a combination of lowercase letters, uppercase letters, digits, and underscores, but must not start with a digit.

magical_beast = "Dragon"

Keywords are special reserved words that have a specific meaning to the Python interpreter. They're like the sacred words in our spells - you can't use them as identifiers. Words like for, if, else, while, def, return are all keywords.

Indentation is crucial in Python. It's used to define a block of code. Unlike other languages that use braces {}, Python uses indentation. This makes Python code easy to read… and quite poetic!

for i in range(5):
    print("Hogwarts")

Variables and data types

Variables are like magical containers where we store our data. Python has several types of data that can be stored in these containers.

my_integer = 7
my_float = 7.0
my_complex = 7 + 7j
my_string = "Hello, Python!"
my_list = [1, "two", 3.0]
my_tuple = (1, "two", 3.0)
my_dictionary = {"name": "Harry", "age": 11}

Operators

Operators are the magical symbols in our spells that perform operations on our variables and values.

With all these, you are now well-equipped to start creating your own Python spells! Remember, practice makes perfect. So, don't forget to keep coding and having fun with it. Happy coding, wizard!

Previous Next

Written © 2024 Written Developer Tutorials and Posts.

𝕏