Statements

 

Statements

 

The next step in learning a Language is learning to build Statements. As already discussed there are 7 types of statements available in a programming language. We will discuss one by one. In python there are no Statement Terminators like ‘.’ Or ‘;’ which are used in other Languages. Every Statement ends at new line character. So when we type in a New Line then the Interpreter automatically recognizes it.

 

1.Comment Statement

 

This statement is not for compiler or Interpreter but for another Human only. This helps in making the Program more easily readable.

 

Comments in Python begin with a hase mark ‘#’ and whitespace character and continue to the end of the line.

Uses

*      Comments can be used to explain python code.

*      Comments can be used to make the code more readable.

*      Comments can be used to prevent execution when testing code.

 

Multi Line Comments

 

Python does not have a specific syntax for multiline comments.

-To add a multiline comment you could insert a # for each line:

-Or, you can use a multiline string, though it is not a correct method.

 

Since Python Interpreter will ignore such String literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it:

 

Example

“””

This is a comment

Written in more than just one line

“””

print(“Hello, Student!”)

 

As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and thus we can create a Multiline comment.

 

2.Declaration Statement

There is no statement for Declaration. The language does not require declaring variables before its usage, like in other Languages. Python does not use Compiler for Memory allocation for variables. Hence declaring the variables is not needed. Memory allocations happen during Runtime only when the Object is created. So based on the Object Data, the Memory is allocated.

 

3.Assignment Statement

 

Expressions using the Assignment Operators are called Assignment statements, As discussed in Assignment Operators. Python has unique feature of assigning values to multiple variables separated by Commas in a single statement at the same time.

 

Example 1

 

a,b,c,d=5,10,15,20

print(a)

print(b)

print(c )

print(d)

 

here is allotted 5,b with 10,c with 15 and d with 20 in the same order. Care must be taken that the LHS count matches with that of RHS count.

 

Example 2

a,b,c,d=5,”kumar”,10+2j,500.45

print(a)

print(b)

print(c )

print(d)

 

It is also possible to assign different types of Data in a Single Assignment Statement.

 

4.Input Statement and Output Statements

Input: It means any information or data sent to the computer from the user through the keyboard or other devices is called input.

Output: It means the information produced by the computer to the user is called output.

 

Both Input and Output statements in Python are in built Functions that comes along with the Language.

 

The syntax for input function is

Input(prompt_message)

 

Python can automatically identify whether the Data entered is a string, number , or list. But the Data received through the input function is always a String only, which can be converted to our desired one using other functions.

 

And the syntax for the output function in python is

print(object(s),sep=separator,end=end,file=file,flush=flush)

 

Normally used to display the output.

Example

a=int(input(“Give a Number”))

b=float(input(“Give a Number with Decimal places”))

if the passed on value is wrong, then it will throw an error.

 

eval() Function

The return type of eval() function is automatically considered based on what argument is passed to the eval() function.

 

If the argument passed is a string, then the eval() function also returns  a string. if you pass a Data such as int, float, complex, string or boolean, it will return the corresponding data type. The eval() function is used as an alternate for typecasting. let  us see an example for eval().

 

a=eval(input("Give a number"))

print("Value is ",a,"and the Object Data class is",type(a))

 

here, if the value given by the user is whole number it will be of type 'int', if the number given has decimal point then the type will be float and if the value is in Complex form then type will be complex.

 

Split Function

 

Split() function breaks a single string into multiple strings using a delimiter.

a,b=input("Enter two number:").split("")

 

And if you wanted to split the numbers with a comma, then you can pass the argument as split(",")

 

a,b=[int(x) for x in input("Enter two numbers;").split(",")]

print("The sum:",a+b)

 

a,b=[float(x) for x in input("Enter the three values with comma separation:").split(",")]

 

How to take inputs for the Sequence Data Types like List, Set, Tuple, etc.


 Output Function

 

Python provides the print() function to display output to the standard output devices. The syntax for the output function in Python is

 

print(object(s),sep=separator,end=end,file=file,flush=flush)

 

Parameters:

value(s): Any value , and as many as you like can be given. They will be converted to string before printed.

 

sep='separator': It is Optional. It specify what to print at the end. Default:'\n'

 

end='end' : It is Optional. It specify what to print at the end. Default:'\n'

file: It is optional. It is an object with a write method. Default: sys.stdout

flush: It is Optional. It is a Boolean, specifying if the output is flushed(True) or buffered (False). Default: False.

 

Formatting Output

 

 In this string, we  can write Python expressions between a {and},that can refer to a variable or any literal value.

 

Example: Python String formatting using F string

 

#Initialising a variable

name="ABC"

#Output

print(f' Hello {name}! How are you?')

 

Output

Hello ABC! How are you?

 

using format()

 

We can also use the format() function, to format our output to make it look presentable. 

 

Using Implicit Position

 

n=input("What is your Name")

en=input("Give the Enrollment Number")

print("The Default order output\n")

print("Hai,{},Welcome to Python Strings Class and your  Enrollment Number is {}".format(n,en))

 

Output

What is your Namekavin

Give the Enrollment Number648

The Default order output

 

Hai,kavin,Welcome to Python Strings Class and your  Enrollment Number is 648

 

Using Position order

print("The Position Order output\n")

print("Hai,{1},welcome to python strings class and your Enrollment Number is {0}".format(en,n))

 

Output

What is your Namekavin

Give the Enrollment Number879

The Position Order output

 

Hai,kavin,welcome to python strings class and your Enrollment Number is 879

>>> 

 

Using Keyword order

 

n=input("What is your Name")

en=input("Give the Enrollment Number")

print("The Keyword Order output\n")

print("Hai,{n},welcome to python strings class and your Enrollment Number is {en}".format(en,n))

 

Example: Python string formatting using format() function

 

#initializing variables

a=20

b=10

#addition

sum=a+b

#subtraction

sub=a-b

#output

print('The value of a is {} and b is {}'.format(a,b))

print('{2} is the sum of {0} and {1}'.format(a,b,sum))

print('{sub_value} is the subtraction of {value_a} and {value_b}'.format(value_a=a,value_b=b,sub_value=sub))

 

Output

The value of a is 20 and b is 10

30 is the sum of 20 and 10

10 is the subtraction of 20 and 10

>>> 

 

Using % Operator

 

We can use '%' operator. % values are replaced with zero or more value of elements. The formatting using % is similar to that of 'printf' in C programming Language.

 

%d - integer

%f - float

%s - string

%x - hexadecimal

%o - octal

 

Example

 

#Taking input from the user

num=int(input("Enter a value:"))

add=num+5

#output

print("The sum is %d"%add)

 

Output:

Enter a value:100

The sum is 105

 

Another Example;

 

a=10

b=15

print("The Result of addition of %d+%d is %d"%(a,b,a+b))

 

Output:

 

The Result of addition of 10+15 is 25

 

No comments:

Post a Comment