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.
- Numbers can be of type int (integer), float (decimal), or complex.
my_integer = 7
my_float = 7.0
my_complex = 7 + 7j
- Strings are sequences of characters, enclosed in either single quotes
''
or double quotes""
.
my_string = "Hello, Python!"
- Lists are ordered collections of items (which can be of different types), enclosed in square brackets
[]
.
my_list = [1, "two", 3.0]
- Tuples are like lists, but they are immutable (you can't change their elements once assigned). They are enclosed in parentheses
()
.
my_tuple = (1, "two", 3.0)
- Dictionaries are unordered collections of key-value pairs, enclosed in curly braces
{}
. They're like magical chests where each item has a unique key.
my_dictionary = {"name": "Harry", "age": 11}
Operators
Operators are the magical symbols in our spells that perform operations on our variables and values.
- Arithmetic operators like
+
,-
,*
,/
,%
,**
,//
are used to perform mathematical operations. - Comparison operators like
==
,!=
,<
,>
,<=
,>=
are used to compare values. - Assignment operators like
=
,+=
,-=
,*=
,/=
,%=
,//=
,**=
are used to assign values to variables. - Logical operators like
and
,or
,not
are used to combine conditional statements. - Membership operators like
in
,not in
are used to test if a sequence is presented in an object. - Identity operators like
is
,is not
are used to compare the objects, not if they are equal, but if they are actually the same object.
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!