Variable and Escape sequences
Escape Sequences
Suppose, you want to have a string which contains a single quote (’), how will you specify this string? For example, the string is “What’s your name?”. You cannot specify ‘What’s your name?’ because Python will be confused as to where the string starts and ends. So, you will have to specify that this single quote does not indicate the end of the string. This can be done with the help of escape sequence. You specify the single quote as \’ : notice the backslash. Now, you can specify the string as ‘What’s your name?’.
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. An example is:
’This is the first line\nThis is the second line’ Another useful escape sequence to know is the tab: \t. There are many more escape sequences but I have mentioned only the most useful here.
Raw String
If you need to specify some strings where no special processing such as escape sequences are handled, then what you need is to specify a raw string by prefixing r or R to the string. An example is:
r"Newlines are indicated by \n"
Variable
Variables are used for storing any information and manipulate them as well. Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer’s memory where you store some information.
Identifier Naming
Variables are examples of identifiers. Identifiers are names given to identify something. There are some rules you have to follow for naming identifiers:
- The first character of the identifier must be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore (_).
- The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores (_) or digits (0-9).
- Identifier names are case-sensitive. For example, myname and myName are not the same. Note the lowercase n in the former and the uppercase N in the latter.
- Examples of valid identifier names are i, name_2_3.
- Examples of invalid identifier names are 2things, this is spaced out, my-name and >a1b2_c3.
NOW TRY SOME EXAMPLE GIVEN BELOW BY COMMENTING DIFFERENT PARTS OF THE PROGRAM
i = 5
print(i)
i = i + 1
print(i)
s = '''This is a multi-line string.
This is the second line.'''
print(s)
# SEE THE OUTPUT PRODUCED BY THIS CODE
#NOTE:IN PYTHON WE ASSIGN VALUES TO VARIABLES BY USING = OPERATOR AS SHOWN ABOVE
# HERE IN PYTHON WE DO NOT NEED TO SPECIFY THE TYPE OF THE VARIABLE
i = 5
print(i)
#above example effectively same as
i = 5;
print(i);
#which is also same as
i = 5; print(i);
#and same as
i = 5; print(i)
Output
5
6
This is a multi-line string.
This is the second line.
5
5
5
5
Indentation
Whitespace is most important in Python. This is called indentation. Leading whitespace (spaces and tabs) at the beginning of the line is used to determine the indentation level of the 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 lessons.
One thing you should remember is that wrong indentation can give rise to errors.