Python Syntax

Python Syntax

Most of the syntax is quite similar to other programming languages except it looks more simplified while reading the code.

Simple Hello World

print("Hello World! Welcome to TutorialBrain")

Output

Hello World! Welcome to TutorialBrain

The function “print” is used to print the output of the program.

It is not required to add a semicolon to terminate a statement. However, if you add a semicolon between multiple commands or statement, it will separate them.

print("Hello World!");xyz = 10; print(xyz)

In this case, there will be 3 separate statements in the line.

  1. print(“Hello World!”)
  2. xyz = 10
  3. print(xyz)

Identifiers in Python

Python identifies a name given to a variable, function, class, etc and that name is called an identifier.

Identifier rules

A Python identifier can be letters, alphabets, underscore or a combination of these.

Python restricts the use of punctuation in identifiers. Also, the use of spaces in a single identifier is not valid. Generally, underscores are used to represent space. Some valid and invalid identifiers are given below –

Valid identifiers: ‘std’, ‘std12’, ‘trained_data’, etc.

Invalid identifiers: ‘@gmail_id’, ‘trained data’, etc.

Python is a case sensitive language. So, ‘trained_data’ and ‘Trained_data’ are two different identifiers.

Reserved Words

There are some predefined words that are reserved keywords in the Python programming and cannot be used as identifiers.

List of Reserved Keywords in Python are

Reserved KeywordsDescription
andLogical ‘AND’ operator
asUse this keyword to create an alias
assertIn debugging mode, it is used to rest if a condition if true or false
breakTo break out the flow in a for loop or a while loop
classTo create an object constructor called class
continueThis is used to end the current iteration in the loop and continue from the next iteration
defUse this reserved word to define a function
delThis keyword deletes the objects like part of a list, complete list, variables, etc
elifIt is shorthand for else if so it is use in conditional statements.
elseIf the condition is false, then the control goes to the statement in ‘else’ and executes it
exceptIt is used in try..except to decide what to do in case an exception occurs
False This keyword is a Boolean value of False while doing a comparison
finallyThe block of code inside the finally section will execute without thinking whether an exception occurred or not
forTo create a for loop
fromTo import a particular part of a module
globalThis is used to create a global variable inside a function
ifThis tests the conditional if statement. If the condition is true, the statement under this is executed
importImports a module or a group of module
inTo check if  a value is present within a list, tuples, range, etc. In case of for loop, it is used to loop in a sequence
isTo test if 2 variables refers to the same objects such as a variable
lambdaThis will create an expression that contains arguments. This acts like a small function.
NoneThis reserved keyword defines a null value(no value)
nonlocalIn nested functions, a nonlocal variable is defined to mark that variable as nonlocal which makes that variable belong to the parent function
notLogical ‘NOT’ operator
orLogical ‘OR’ operator
passTo simply pass the flow to avoid getting any error
raiseThis sops the execution of program and raises an exception or an error
returnTo return the value of a function and exit from it
True This keyword is a Boolean value of True while doing a comparison
tryIt is used in try..except to test an expression and decide what to do in case an exception occurs
whileTo create a while loop to test a condition before the loop executes
withUsed in Exception handling to wrap up the execution of a group of code inside methods
yieldInside a function, it is similar to a return statement but it returns a generator(1 item at a time in loop)

Statement Termination

A statement in Python ends when the current line is switched. However, the character (\) can be used to continue the statement in the next line.

For example:
~~ Sum = Variable1 + \
Variable2

This adds both the variables instead of terminating the statement in the first line.

Also, you can use multiple lines in (), {} and [] brackets without terminating the code in the first line.

Taking Inputs

To take inputs from the user, the function ‘input’ is used just like the print function.

For example
The below code will take input from the user after displaying possible output –

~~ std = input("Enter the name of your Favourite Website")
~~ print("The name of my favorite website is ", std)

Running a script in Python

The interpreter in python stops executing when the script ends and it becomes inactive after executing the whole program.

If you type the first code in a script named first_script.py and run the following code

~~ python first_script.py
Before running the program, set the path directory correctly, it will provide the same output as as compared to its run in command line.

Other basic Python syntax

The quotation is used to make strings in python. The language won’t restrict you to use multiple parentheses to write a string. (‘), (“), (‘’’) and (“””) can be used to define a string and for other purposes in Python. Generally, triple quotes are used for the strings that extend to multiple lines.

To give comments in Python, hash (#) is used & the statement with a hash is totally ignored by the language while executing the program.

Also, a blank line with or without a comment is also ignored while execution. The function ‘\n’ can be used in a string to escape into a new line in the output.

Tutorials for all brains!