subjects

Dropdown Menu

Monday, 22 February 2021

Python Statements

 

A statement is an instruction that a Python interpreter can execute. In python we use various statements to write the code. Some of the statements available in Python are assignment statement, print statement, control flow and the import statement.

Assignment Statement:

  Assignment statements are used to set value to a variable

Ex: a = 10

in this statement value 10 is  stored into variable a.

we can use assignment statement in evaluating an expression

     f  = c *9.0/5.0 + 32

This expression converts Celsius into Fahrenheit

 

We can assign several variables at once

  a = b = c =10

 

Python permits number of variables to assign with different values with comma separated values

                   x , y = 10,20

is same as

                   x = 10

y = 20

 

Input and Output Statements

 

Input from the user: We use input function to read data from user,

>>>input(“Enter a number”)

…Enter a number 10

'10'  Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions.

>>> int(10)

10

>>>float(10)

10.0

Output:

print statement: In python  we use  print statement to display output in console

>>> print(“Hello world”)

will display Hello world as  output

formatting output

The print statement produces a more readable output, by omitting the enclosing quotes and by printing escape sequences, comma separator and special characters:

 

 \'     display   '                            \"   display        "\t  tab space                \n new line

No comments:

Post a Comment