Basic Syntax
Python Basics • Last Updated: 01/12/2025
Python Basics • Last Updated: 01/12/2025
When coding in Python, there's a key syntax you need to follow. This syntax is like the language your device understands. Python as a coding language was developed then released in 1991 as an English look-alike to make it easy to learn, making it one of the first languages any programmer jumps to when beginning their journey.
When coding in Python, you need to understand syntax. Operations need to be shown as certain symbols, and they follow the order of operations for calculations, (which will be covered later here). Brackets are extremely useful when you need to separate items for the order of operations, covered here.
This cannot be overstated enough, and it is REALLY important for how you use them. When you use quotations in Python, it must be only one type for a string or another. Those are the single and double mark, they are interchangeable. The example below shows just how they should be used when you have to deal with strings:
... notice the mismatch in quotation marks in the invalid code? Mismatches in quotations will cause Python to throw an error, so it will fail to run the rest of the code so watch out for that!
Some really useful functions you'll find yourself using a lot on Command Prompt output code is the print() command, and the input() command. You'll also find yourself using str() and int(), but not as much as print() and input().
The way you'd put them in code basically look like this:
As shown above on how to use it, the print command does one thing: it takes any string (text) that it finds within quotations and prints it to the output. Like this:
This is also what you would see in your command prompt when you use an input() command -- except for the fact that you have to type text in. A really important factor is that an input prompt must be assigned to a variable. More on variables will be covered over here. You can find an example of a valid usage of input prompts below through a temporary variable.
As the heading suggests, this section of this page will cover how integers and strings can be manipulated to your needs.
You can form integers from the int() command. If it is possible for the value to be a decimal (has a decimal point), then use float() instead, otherwise Python WILL throw an error. Here's an example of how you can use this to your will:
... if you think about it, floats are literally just a way of saying: "decimal values that aren't whole numbers" -- which in standard terms is exactly what it is. On mathematical terms, 'decimal' just means 'number', so 'floats' are used instead.
Well, here we are. Manipulating strings is simple -- all you have to do is use the str() command. This command will take a value from the given integer or float or string, and convert that into a string value, so it cannot be treated as a number (or Python will throw an error).
...and that just about sums up all basic syntax that you would need to know before learning Python code + basic commands that will be really useful. Anything you might not know already from this page (like functions or loops) will be covered in later topics.
If you have any questions, please head to the comments section below!
Share your opinions or ask questions.
-