A physical line is what you see when you write the program. A logical line is what Python sees as a single statement. Python implicitly assumes that each physical line corresponds to a logical line. If you want to specify more than one logical line on a single physical line, then you have to explicitly specify this using a semicolon ( ; ) which indicates the end of a logical line/statement. For example, all the 4 examples shown below have 2 logical lines:
However, I strongly recommend that you stick to writing a maximum of a single logical line on each single physical line. The idea is that you should never use the semicolon.
Python3 has a built-in function input() to accept user input. The input() function reads a line entered on a console by an input device such as a keyboard and convert it into a string and returns it. As a new developer, It is essential to understand what is input in Python.
print(‘Enter your name:’) x = input() print(‘Hello, ‘ + x)
x = input(‘Enter your name:’) print(‘Hello, ‘ + x) |
The Input is nothing but some value from a system or user. For example, if you want to perform an addition of two numbers on the calculator you need to provide two number to the calculator, those two number is nothing but an input provided by the user to a calculator program. Whatever you enter as input, the input() function converts it into a string. If you enter an integer value, still it will convert it into a string. If you want to number input from a user, you need to perform type conversion on the input value.
#Input from the user
#Example 2 ############## Assignment: use INPUT function |
You cannot specify ‘What’s your name?’ because Python will be confused as to where the string starts and ends. To specify that this single quote does not indicate the end of the string, we use escape sequence. In Python : backslash character is used as escape sequence. Also, you have to indicate the backslash itself using the escape sequence \ . To insert characters that are illegal in a python string, we use an escape character. An example of an illegal character is using apostrophy s (‘s) a string quoted in single quote, as discussed earlier in this paragraph. For the same reason its illegal to use double quote in a string that begins and ends with double quote. Then how do we print – What’s your name? One option is use double quote or triple quote to include a text that has single quote as one of its content. Second option is to quote escape sequence, this will make python ignore the single quote and treat it like any other normal character. Escape sequence works on only one character that is followed after itself. So to print “\”, we will have to use “\\” – that’s 4 slashes.
Examples
#Example: using ‘s quote in strong within single quotes |
What if you wanted to specify a two-line string? One way is to use a triple-quoted string as shown previously or you can use an escape sequence for the newline character – n to indicate the start of a new line. Another useful escape sequence to know is the tab: t. One thing to note is that in a string, a single backslash at the end of the line indicates that the string is continued in the next line, but no newline is added.
Examples
#Single print statement to print in two lines |
Look at more programs and refer the comments within the programs to learn more:
name=“Saurav" |
There are many more escape sequences given in the table below. You can practice them and see how the output is generated.
Escape Sequence |
Meaning |
newline |
Ignored |
\ |
Backslash () |
‘ |
Single quote (‘) |
“ |
Double quote (“) |
a |
ASCII Bell (BEL) |
b |
ASCII Backspace (BS) |
f |
ASCII Formfeed (FF) |
n |
ASCII Linefeed (LF) |
r |
ASCII Carriage Return (CR) |
t |
ASCII Horizontal Tab (TAB) |
v |
ASCII Vertical Tab (VT) |
ooo |
ASCII character with octal value ooo |
xhh… |
ASCII character with hex value hh… |
Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called indentation. Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements. This means that statements which go together must have the same indentation. Each such set of statements is called a block. We will see examples of how blocks are important in later chapters.
One thing you should remember is that wrong indentation can give rise to errors. For example:
|
|
Output:
|
|
Some Pragrams to practice along and analyze the output based on the chapter lessons:
print(“Hello")
#### Escape Sequence
|
Python also allows you to assign several values to several variables within the same line. Each of these values can be of a different data type.
Example
j, k, l = “shark", 2.05, 15 |