Master Python Syntax Simplified: A Comprehensive Guide

In this tutorial, we’ll show a overview of Python syntaxes.

Write First Python Program

So that we have Python up and running, we can make our first Python program.

Now let,s create a very simple program called Hello World. A “Hello, World!” is a easy program that outputs Hello, World! on the system screen. Because it’s a very simple program, it’s often used to institute a new programming language for beginners.

Write the following code in any text editor or an IDE and save it as hello_world.py


print("Hello, world!")

After, run the file. You will find the following output.

Hello, world!

Congratulations! You just have wrote your first program in Python.

Keywords and Identifiers In Python

Python Keywords

Keywords are predefined, conserved words used in Python programming that have particular meanings to the compiler.

All of the keywords without True, False and None are in lowercase and they must be written so they are. The list of all keywords is given below.

Screenshot-2024-01-25-035241

glancing at all the keywords at once and trying to figure out what they build might be Irresistible.

Identifiers are the name of given to variables, classes, methods, etc. For example-

language = 'Python'

Above, language is a variable (an identifier) as holds the value 'Python'.

You cannot use keywords as variable names which they are reserved names that are built-in to Python. So For example-

continue = 'Python'

The upon code is false because you have used continue as a variable name.

Rules for Naming an Identifier In Python

  • Identifiers be unable to a keyword.
  • Identifiers are also case-sensitive.
  • It can have a alternation of letters and digits. Still, it must start with a letter or _. And the first letter of an identifier cannot be a single digit.
  • It’s a symposium to start an identifier with a letter rather _.
  • Whitespaces are’t allowed.
  • We cannot use individual symbols like !, @, #, $, and instantly.

Some Valid and Invalid Identifiers

Screenshot-2024-01-25-040558

Comments In Python

Comments are indication that we add to our code to create it easier to understand.

When acting code, Python’s interpreter disregard comments.

Now for example, yow have a program to print a text entered by the user.

name = input("Enter your name")
print(name)

To create this program more readable, you can add comments like this-

# Program to take the user's name

name = input('Enter your name')
print(name) 

Below, the line starting with # is a comment. The Python compiler disregard everything after the # symbol.

Single-line Comment

We can use the hash(#) symbol to write a single-line comment. Now for example-

# declare a variable
name = 'John'

# print name
print(name)    # John

In the upon example, you have used three single-line comments:

# declare a variable # print name # John

You can also use single-line comments side by side the code:

print(name)    # John

Multiline Comments

Python doesn’t have dedicated to multi-line comment syntax like as some other programming languages like as C++ and Java.

Still, we can acquire the same outcome by using the hash (#) symbol at the beginning of every line.

Now Let’s look at an example-

# print(1)
# print(2)
# print(3)

You can further use multiline strings as comments like as-

'''This program takes an input from the user
and prints it'''

name = input('Enter your name: ')
print(name)

Output

Enter your name: John
John

Prevent Executing Code Using Comments

Comments are valuable when debugging program code.

Supposing you encounter an error while running a program, instead of perishable code segments, you can comment them out to confine performance.

For Examples

number1 = 10
number2 = 15

sum = number1 + number2

print('The sum is:', sum)
print('The product is:', product)

Below, the code throw an error from we have not defined a product variable.

number1 = 10
number2 = 15

sum = number1 + number2

print('The sum is:', sum)
# print('The product is:', product)

Output

The sum is 25

Variables, Constants and Literals In Python

Python Variables

In programming, a variable is a bridegroom (storage area) to hold data. For example-

number = 10

Below, number is the variable storing the value 10.

Assigning values to Variables in Python

As you can see from the upon example, you use the employment operator = to assign a value to a variable.

# assign value to site_name variable
site_name = 'programiz.pro'

print(site_name)

# Output: programiz.pro

Changing the Value of a Variable in Python Program

site_name = 'devsenv.pro'
print(site_name)

# assigning a new value to site_name
site_name = 'apple.com'

print(site_name)

Output :

devsenv.pro
apple.com

Example: Assigning many values to multiple variables

a, b, c = 5, 3.2, 'Hello'

print(a)  # prints 5
print(b)  # prints 3.2
print(c)  # prints Hello 
Previous
Python Programming - Introduction to Python For Beginners
Next
Hello World In Python - First Programming Example in Python