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
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.
- print(“Hello World!”)
- xyz = 10
- 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 Keywords | Description |
and | Logical ‘AND’ operator |
as | Use this keyword to create an alias |
assert | In debugging mode, it is used to rest if a condition if true or false |
break | To break out the flow in a for loop or a while loop |
class | To create an object constructor called class |
continue | This is used to end the current iteration in the loop and continue from the next iteration |
def | Use this reserved word to define a function |
del | This keyword deletes the objects like part of a list, complete list, variables, etc |
elif | It is shorthand for else if so it is use in conditional statements. |
else | If the condition is false, then the control goes to the statement in ‘else’ and executes it |
except | It 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 |
finally | The block of code inside the finally section will execute without thinking whether an exception occurred or not |
for | To create a for loop |
from | To import a particular part of a module |
global | This is used to create a global variable inside a function |
if | This tests the conditional if statement. If the condition is true, the statement under this is executed |
import | Imports a module or a group of module |
in | To 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 |
is | To test if 2 variables refers to the same objects such as a variable |
lambda | This will create an expression that contains arguments. This acts like a small function. |
None | This reserved keyword defines a null value(no value) |
nonlocal | In nested functions, a nonlocal variable is defined to mark that variable as nonlocal which makes that variable belong to the parent function |
not | Logical ‘NOT’ operator |
or | Logical ‘OR’ operator |
pass | To simply pass the flow to avoid getting any error |
raise | This sops the execution of program and raises an exception or an error |
return | To return the value of a function and exit from it |
True | This keyword is a Boolean value of True while doing a comparison |
try | It is used in try..except to test an expression and decide what to do in case an exception occurs |
while | To create a while loop to test a condition before the loop executes |
with | Used in Exception handling to wrap up the execution of a group of code inside methods |
yield | Inside 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.