Home
About Us Privacy Policy
 

SYNTAX

ADVERTISEMENT

Beginner Syntax



One of the Python programming languages' significant advantages is that its syntax is straightforward to learn and understand, even for novices.

Here is an example of printing a statement.

Quickly, open the command line or terminal and execute command python.

print("I love Python programming language!.")

I love Python programming language!.

Let's say you might want to display something on the screen. If you were to tell somebody to do that, how would you say it?

Print what time is it?

As explained earlier, Python's syntax is very English-like, and here is the equivalent version of the above in Python's syntax.

print("What time is it?")

Let us have a look at an another example, shall we?

If today is Monday and it's not a holiday, then I have to go to work.

Now, compare the similarity of the above statement in English with Python's code .

if today == "Monday" and not holiday:
    goto_work()

The above example demonstrates Python's concise syntax, making it easier for programmers to write and manage codebases, resulting in a better development strategy, fewer bugs, and low cost.


ADVERTISEMENT

Philosophy

Before moving any further, let's understand the philosophy behind Python.

The "Zen of Python" is a short poem written by Tim Peters that briefly explains its design philosophy and ability.

Open the terminal or IDLE(Integrated Development and Learning Environment) and type the following:

import this

The Zen of Python, by T...(redacted to avoid plagarism)


ADVERTISEMENT

Clarity of Code

When developing software or maintaining legacy codebases, clarity and readability define the quality of the software and are of utmost importance. Lets, consider a real-world example to help understand the significance.

Suppose you have to answer a question in an exam, you have two choices, either take your time to properly construct your answer and deliver it properly in good handwriting or write it as fast as you can to save time to get over with it. Most people would take the first option instead of the second because not only will you come up "hopefully" with sound arguments and points, but you will also be intelligible.

Python highly appreciates this philosophy and enforces it with high-quality software engineering principles with decades of tried and tested research behind it to help develop a clear, intelligible, concise, maintainable codebase.

Let's go through a few common examples involving other popular programming languages and compare them with Python to see the difference.

Language Code Python Equivalent % Smaller
(by no. of characters)
C++
#include <iostream>
using namespace std;
int main () {
  std::cout << "learnpython.tech" << std::endl;
  return 0;
}
print("learnpython.tech")
                                        
78%
Rust
fn main() {
    println!("learnpython.tech");
}
print("learnpython.tech")
                                        
42%
Java
public class HelloWorld {
    public static void main(String[] args) {
      System.out.println("learnpython.tech");
    }
}
print("learnpython.tech")
78%
PHP
echo 'learnpython.tech';
print("learnpython.tech")
0%
Go
import "fmt"
func main() {
    fmt.Println("learnpython.tech")
}
print("learnpython.tech")
59%
Ruby
puts "learnpython.tech"
print("learnpython.tech")
0%

As you can extrapolate from the examples above, Python's version is the easiest to understand while achieving the same outcome. Additionally, it's also faster to develop, easier to maintain, less bug-prone than the others.


ADVERTISEMENT
Indentation

Python's most impressive feature it's clean syntax, mainly due to the absence of opening/closing braces to group-dependent logical statements as in many other C-based languages. Although Python is a C-Based language, meaning it has taken design inspiration from the C Programming Language. However, its syntax is very different from the latter. Indentations in Python have a particular purpose, unlike other languages. They are used to group a sequence of logically linked statements, a.k.a defines a block-of-code.

A block-of-code is a group of statements that are linked logically to each other.

  • Indentation should be a multiple of 2 spaces/tabs as a general rule.
  • Always prefer the use of tabs that expands to spaces when using indentation.


Examples

Example 1:

a_number = 8
if a_number == 8:
    print("The number is 8")

The number is 8

Example 2:

color = "royal purple"
if color == "red":
    print("Red")
elif color == "green":
    print("Green")
elif color == "royal purple":
    print("Royal Purple")
else:
    print("Unknown color")

Royal Purple

Example 3:

def say_hello():
    print("Hello!")

say_hello()

Hello!


Conclusion

In this chapter, we learned about the Python programming language's English-like, expressive, yet simplistic syntax allowing for unparallel productivity while keeping the codebase maintainable and scalable.


ADVERTISEMENT



All product names, logos, and brands are property of their respective owners.