Python Statements

What is Python Statement

For creating any type of program, instructions shall be written in logical manner. Variety of Python instructions, which, are written for execution are called Python statements.

Example –

print(“Hello World”) – This is a statement to print Hello World on the standard screen.

demoVar = 123 – This is a statement to assign value 123 to variable demoVar.

Types of Python Statement

There are different types of Python Statement. Majorly statements are categorised into 3 parts as follows –

  • Assignment Statements
  • Conditional Statements
  • Looping Statements

1. Assignment Statements –

Statements which are used to assign values to variables are called assignment statements. Any type of data can be assigned to a variable. No need for mentioning the type of data.

Example –

demoIntVar = 43 – This is a statement to assign value 43 to variable demoIntVar.

demoStrVar = “My String Variable” – This is a statement to assign value “My String Variable” to variable demoStrVar.

2. Conditional Statements –

When there is a need to take some actions based on certain conditions; conditional statements are used.

Example –

Scenario –

XYZ company has made an announcement -> Incase any employee’s salary is less than 20,000 per month then increase salary by 10%

Conditional Statement Code ->

empSal = 19000
if( empSal < 20000 ):
      empSal = empSal + empSal * 10/100
      print(empSal)

Explanation –

In the above code, if statement is used to specify the condition for employee salary and if condition is true then only employee’s salary is increased.

3. Looping Statements –

When there is a need to write repetitive code; loops shall be used.

Example –

Scenario –

Calculate the sum of 1st 10 numbers starting from number 1.

Looping Statement Code ->

sum = 0
for numberCount in range(1,11):
    sum = sum + numberCount
print(sum)

Explanation –

In the above code, for statement is used to loop through the repetition statement.

Multi-Line Statements

Statements which are very long and doesn’t fit into single line/ or are note adhering with defined coding standards will be spilled over multiple lines. In order to achieve this, multi-line statement will be used.

There are many ways to write multi-line statements, which are listed below –

  • Parentheses – ()
  • Curly Braces – {}
  • Square Brackets – []
  • Semicolon – ;
  • Backslash – \

Examples for using above methods are tabulated below –

Multi-line CharacterCode Snippet
()demoVarOne = ( 22 * 8 – 9
+ 12 +9 )
{}demoVarOne = { 22 * 8 – 9
+ 12 +9 }
[]demoVarOne = [ 22 * 8 – 9
+ 12 +9 ]
\demoVarOne =  22 * 8 – 9\
+ 12 +9
;demoVarOne = 1 ;  demoVarTwo = 2;  demoVarThree = 3
Multi-line Statement Example

Video tutorial for Python Statements can be accessed here

#python #pythonstatement #pythonforbeginner #python3 #pythonmultilinestatement #programming #coding #developer #howtowritepythonstatement

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *