You are on page 1of 11

STATEMENTS

Statements in Python typically end with a new line. Python does, however,
allow the use of the line continuation character (\) to denote that the line should
continue. For example −
total = item_one + \
item_two + \
item_three
Statements contained within the [], {}, or () brackets do not need to use the
line continuation character. For example −
days = ['Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday']

In Python, the statements are usually written in a single line and the last
character of these lines is newline. To extend the statement to one or more lines
we can use braces {}, parentheses (), square [], semi-colon “;”, and continuation
character slash “\”. we can use any of these according to our requirement in the
code. With the line continuation character, we can explicitly divide a long
statement into numerous lines (\). 
Code:
 Python3

# Breaks the lines using continuation character

g = "geeks\

for\

    geeks"

print(g)

In the above code if we do not use the continuation characters the code will give
unterminated string literal error.
Output:

geeksforgeeks

Line continuation are divided into two different ways:


 Explicit line continuation
 Implicit line continuation
Explicit line continuation:
In this type of multi-line statement, we will be using the line continuation
character (\) to split a statement into multiple lines.
Example:
In this example, we are initializing the text, and the mathematical expression
using the ‘\’ sign which is the explicit line continuation to continue the same line
in the multiple lines in python programming.

 Python3

# Initializing a text using the

# Explicit multi-line statement.

text = "A Computer Science portal\

for geeks. It contains well written, well \

 thought and well explained \

 computer science and programming \

  articles"

print('\n Initializing a text using\


    the Explicit multi-line statement', text)

  # Initializing a mathematical expression

# using the Explicit multi-line statement.

add = 50 + \

    40 - \

    52

  print('\n Initializing a mathematical expression\

    using the Explicit multi-line statement', add)

Output:
Initializing a text using the Explicit multi-line statement A Computer Science
portalfor geeks. It contains well written, well thought and well explained
computer science and programming articles

Initializing a mathematical expression


using the Explicit multi-line statement 38
Implicit line continuation:
In this type of multi-line statement, Implicit line continuation is used when you
split a statement using either parentheses ( ), brackets [ ], and braces { }. 
Example:
In this example, we are initializing the list and the mathematical expression using
the parentheses ( ), brackets [ ], and braces { } sign which is the implicit line
continuation to continue the same line in the multiple lines in python
programming.
 Python3

# Initializing a list using the

# Implicit multi-line statement.

list = [5,

        4, 3, 2, 1

        ]

print('Initializing a list using the\

 Implicit multi-line statement', list)

  

# Initializing a mathematical expression

# using the Implicit multi-line statement.

add = (50 +

       40 -

       52)

  

print('\n Initializing a mathematical expression\


 using the Explicit multi-line statement', add)

Output:
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38

Multiple statement groups as suites in python

A group of individual statements, which make a single code block are called
suites in Python. Compound or complex statements, such as if, while, def, and
class require a header line and a suite.
Header lines begin the statement (with the keyword) and terminate with a colon
(: ) and are followed by one or more lines which make up the suite. 

example

if expr1==True:
  stmt1
  stmt2
elif expr2==True:
  stmt3
  stmt4
else:
  stmt5
  stmt6
while expr==True:
  stmt1
  stmt2
QUOTES IN PYTHON

Quotation symbols are used to create string object in Python. Python recognizes
single, double and triple quoted strings. String literals are written by enclosing a
sequence of characters in single quotes ('hello'), double quotes ("hello") or triple
quotes ('''hello''' or """hello""").
>>> var1='hello'
>>> var1
'hello'
>>> var2="hello"
>>> var2
'hello'
>>> var3='''hello'''
>>> var3
'hello'
>>> var4="""hello"""
>>> var4
'hello'
If it is required to embed double quotes as a part of string, the string itself should
be put in single quotes. On the other hand, if single quoted text is to be embedded,
string should be written in double quotes.
>>> var1='Welcome to "Python training" from Tutorialspoint'
>>> var1
'Welcome to "Python training" from Tutorialspoint'
>>> var2="Welcome to 'Python training' from Tutorialspoint"
>>> var2
"Welcome to 'Python training' from Tutorialspoint"
Python accepts single ('), double (") and triple (''' or """) quotes to denote string
literals, as long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all
the following are legal −
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

INPUT / OUTPUT and IMPORT Function

Python input() Function

Python input() function is used to get input from the user. It prompts for the user
input and reads a line. After reading data, it converts it into a string and returns
that. It throws an error EOFError if EOF is read.

Signature

1. input ([prompt])  

Parameters

prompt: It is a string message which prompts for the user input.

Return

It returns user input after converting into a string.

Let's see some examples of input() function to understand it's functionality.

Python input() Function Example 1

Here, we are using this function get user input and display to the user as well.

1. # Python input() function example  
2. # Calling function  
3. val = input("Enter a value: ")  
4. # Displaying result  
5. print("You entered:",val)  

Output:

Enter a value: 45

You entered: 45

Python input() Function Example 2

The input() method returns string value. So, if we want to perform arithmetic
operations, we need to cast the value first. See the example below.

1. # Python input() function example  
2. # Calling function  
3. val = input("Enter an integer: ")  
4. # Displaying result  
5. val = int(val) # casting into string  
6. sqr = (val*val) # getting square  
7. print("Square of the value:",sqr)  

Output:

Enter an integer: 12

Square of the value: 144

Input function

In Python, we have the input() function for taking the user input. The input()
function prompts the user to enter data.It accepts all user input as string. The user
may enter a number or a string but the input() function treats them as strings only.
The syntax for input() is:
input ([Prompt])
Prompt is the string we may like to display on the screen prior to taking the input,
and it is optional. When a prompt is specified, first it is displayed on the screen
after which the user can enter data. The input() takes exactly what is typed from
the keyboard, converts it into a string and assigns it to the variable on left-hand
side of the assignment operator (=). Entering data for the input function is
terminated by pressing the enter key.

Example

>>> fname = input("Enter your first name: ")


Enter your first name: Arnab
>>> age = input("Enter your age: ")
Enter your age: 19
>>> type(age)
<class 'str'>

The variable fname will get the string ‘Arnab’, entered by the user. Similarly, the
variable age will get the string ‘19’. We can typecast or change the datatype of the
string data accepted from user to an appropriate numeric value. For example, the
following statement will convert
the accepted string to an integer. If the user enters any non-numeric value, an error
will be generated.

Example
#function int() to convert string to integer
>>> age = int( input("Enter your age:"))
Enter your age: 19
>>> type(age)
<class 'int'>

Python print() Function

Python print() function prints the given object on the screen or other standard


output devices.

Signature

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

Parameters
object(s): It is an object to be printed. The Symbol * indicates that there may be
more than one object.

sep='separator' (optional): The objects are separated by sep. The default value of


sep is ' '.

end='end' (optional): it determines which object should be print at last.

file (optional): - The file must be an object with write(string) method. If it is


omitted, sys.stdout will be used which prints objects on the screen.

flush (optional): If True, the stream is forcibly flushed. The default value of flush
is False.

Return

It does not return any value.

Python print() Function Example 1

The below example shows the working of print().

1. print("Python is programming language.")  
2.   
3. x = 7  
4. # Two objects passed  
5. print("x =", x)  
6.   
7. y = x  
8. # Three objects passed  
9. print('x =', x, '= y')  

Output:

Python is programming language.

x=7

x=7=y
Explanation:

In the above code, only objects parameter is passed to print() function (in all three
print statements).

The end parameter '\n' (newline character) is used to display output in the next line,
and it is by default. As we can see, each print statement displays output in the new
line.

If the file is saved as sys.stdout, then, the output is printed on the screen.

Here the value of flush is False, so the stream is not forcibly flushed.

Python print() Function Example 2

The below example use print() with separator and end parameters.

1. x = 7  
2. print("x =", x, sep='00000', end='\n\n\n')  
3. print("x =", x, sep='0', end='')  

Output:

a =000007

a =07

You might also like