Python Lines
Table Of Contents:
- What Are Python Lines ?
- Joining Multiple Lines To Form A Single Logic.
- Multiple Statements On A Single Line.
(1) What Are Python Lines?
- A Python program is divided into a number of logical lines and every logical line is terminated by the token NEWLINE.
- A logical line is a collection of statements, that comes under a single logic implementation.
- Python interpreter searches for the ‘Newline’ token to find out where logic ends.
- Split a Logical line into multiple physical lines using Semicolon (;) and Join multiple physical lines into one logical line using (\).
- A physical line in your program file is a new line, where a new logic starts.
(2) Joining Multiple Lines To Form A Single Logic:
- When you want to write a long code in a single line you can break the logical line into two or more physical lines using the backslash character(\).
- Therefore when a physical line ends with a backslash character (\) and not a part of a string literal or comment then it can join another physical line.
- See the following example.
Example-1
value = (54 +36) + (54 +36)+ (54 +36)+ (54 +36)+ (54 +36) + \
(54 +36) + (54 +36)+ (54 +36)+ (54 +36)+ (54 +36) + \
(54 +36) + (54 +36)+ (54 +36)+ (54 +36)+ (54 +36)
print(value)
Output: 1350
Example-2
a = 0
b = 1
c = 2
d = 3
e = 4
f = 5
if a==0 and b > 1 and \
c > 2 and d==3 and \
e < 5 and f < 3:
print("Example Of Line Joining")
(3) Multiple Statements On A Single Line:
- You can write two separate statements into a single line using a semicolon (;) character between two lines.
- It will allow you to write multiple statements into a single line.
Example-1
string1 = "How Are You ?"
string2 = "I Am Fine ! Thank You"
print(string1) ; print(string2)
Output:
How Are You ?
I Am Fine! Thank You
Example-2
a = 0 ; b = 1 ; c = 2 ; d = 3 ; e = 4 ; f = 5
sum = a + b + c + d + e + f
print(sum)
Output: 15