Basics Syntax in Python
- The syntax are set rules that defines how Python program will be written so we have to go through the rules while writing Python code.
- Python is actually best programming language for beginners. Its syntax is similar to English, which makes it easy to read and understand.
- Python has syntax that allows programmers to write programs with fewer lines than other programming languages
Table of Content
- Print() Function
- Identifiers
- Keywords
- Comments
- Indentation
Print()
- The print() function prints the given parameter value to the output screen.
- For example : print message to the screen
> Print("Hello World")
Output : Hello World
Here we write our first Python program
Identifiers
- Identifiers is a name used to identify a variable, function class, module or other object
- Python is a case sensitive language.
- Case sensitive means it treats uppercase and lowercase characters differently
- You must avoid same name while naming to identifiers.
- Rules for creating identifiers:
- Starts with uppercase and lowercase alphabet or an underscore
- An identifiers cannot be start with digit or any type of special symbols like #,@,!,%,$ etc
- Keywords cannot be used as identifier.
Keywords
- Keywords are special reserved words that have specific meanings and purposes
- Almost all keywords are in lowercase
- There are 33 keywords in python while creating this tutorial
False continue from not
def class global or
True if pass elif
and raise else in
as except is try
assert lambda while for
None del import return
nonlocal break finally with
yield
Comments
- Comments are used to explain the Python code and to make it more readable.
- With the help of commenting we prevent execution when testing code.
- Once you comment the lines that cannot be executed with other code.
- There are two types of comments in Python programming as follows:
1. Single line Comments
2. Multiple line Comments
1. Single line Comments
- Single line Comments begin with #.
- Single line Comments comment only a inline code it cannot effect on next line.
Example :
> # This is comment now it cannot be executed
> print("Computer Tips & Tricks")
Output : Computer Tips & Tricks
2. Multiple line Comments
- There are no built-in mechanism for multi-line comments in python.
- To comment out multiple lines you can prepend each line with hash or you can comment using triple quotes
Example :
> " " " Multiple line comments are perform using triple
> quotes like this " " "
> print("Computer Tips & Tricks")
Output : Computer Tips & Tricks
Indentation
- Indentation is used to indicate the level of nesting within code blocks it is a technique that provides readers sense of continuity
- Indentation matters! Don't use it casually.
- Following code will report an error 'Unexpected indent'.
Example :
> a = 20
> b = 45
- Additionally, Python uses indentation to define blocks code, such as loops and functions.
- For example, the code block within a for loop must be indented one level more than the for statement
Example :
> for i in range(10):
> print(i)
print("Hello World !")
No comments:
Post a Comment