Latest posts

10/recent/ticker-posts

Basics of Python and Syntax of Displaying Statments

Basics of Python and Syntax of Displaying Statments

What is Syntax ?

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 

  1. Print() Statement
  2. Identifiers
  3. Keywords
  4. Comments
  5. Indentation

Print( ) Statement

To display any text, sentence, or we can say that string, by using Python, we uses print( ) Statement.
The print() function prints the given parameter value to the output screen.

Syntax : 
 print(parameter)

Example :
 print("Hello Learners !")


 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:

  1. Starts with uppercase and lowercase alphabet or an underscore
  2. An identifiers cannot be start with digit or any type of special symbols like #,@,!,%,$ etc
  3. 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.

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

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 

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.
In that python interpreter automatically adds 4 spaces before the function body content or loop content.

Example : 
 for i in range(1,10+1):
     i = str(i)
     print(i + ") " + i)
    
 """
 Output : 
    
 1) 1
 2) 2  
 3) 3  
 4) 4  
 5) 5  
 6) 6  
 7) 7  
 8) 8  
 9) 9  
 10) 10

 """


When we doesn't uses indentation then the compiler shows errors.

Example,
 for i in range(1,10+1):
 i = str(i)
 print(i + ") " + i)

Error,
     i = str(i)
     ^
 IndentationError: expected an indented block after 'for' 
 statement on line 1

So to avoid this type of errors, we must need to use indentation !

Learn all the Contents of Table in detail in next Pages.

Hope this post will help you to learn Python More !

Thank You...!!!





Also Visit


Post a Comment

0 Comments