PYTHON-LESSON2
PYTHON-LESSON2
In computer science, a literal is a notation for representing a fixed value in source code. In
contrast to literals, variables or constants are symbols that can take on one of a class of fixed
values, the constant being constrained not to change.
STRING LITERALS
• String – is an array of characters. (alphanumeric)
• Text enclosed in quotes forms a string in python.
• “ab” ”a” ‘ab123’ are all string literals.
• Quotes can be single, double or triple.
• Escape Sequences - Character combinations consisting of a backslash (\)
followed by a letter or by a combination of digits are called "escape sequences."
To represent a newline character, single quotation mark, or certain other
characters in a character constant, you must use escape sequences.
• Mostly used for producing non printable (non graphic) characters and some
special characters(characters used by the language as part of a syntax) in your
output screen.
ESCAPE SEQUENCES
\n– NEW LINE
TYPES OF STRINGS
single line and multiline
• Single line strings(Basic strings) : The strings which is enclosed with single or
double quotes are single line strings. They must end in one line.
• Valid string: str1=“welcome”
• Str2 = ‘To Python’
• Invalid single line string.
• Text1 = “Hello. // Will throw error because
World” // enter pressed without closing quote.
Text1 = “Hello\
World”
Even though written in 2 lines (seperated with a \) the string is considered as
continuous. i.e. the string will be printed as Helloworld in output screen.
Multiline String
• Multiline strings are created by typing the text in triple quotation marks
( no backslash needed at the end of line.)
• Eg:
str1 = ”””Hello
World.
Here I Come!!!”””
Print(str1)
• Output
Hello
World.
Here I Come!!!
SIZE OF STRINGS
• ‘\\’ – size is 1. (as \\ is an escape sequence)
• ‘abc’. - size is 3
• “\ab” – size is 2
• “Seema\’s pen” – size is 11
• ”Amy’s” – size is 5.
• (Python allows a single quote (without escape sequence format) in double quoted string and vice versa.
• Nstr = “’he\
ll\
O’’’. - size is 5. \ is not counted in the size of the string.
Nstr = ‘’’HE
LL
O’’’. - size is 7. as enter key(new line) is counted.
Str2= ‘a\
b\
c’. -size is 3. backslash is not counted.
SIZE OF STRINGS
Bytes literals are always prefixed with 'b' or 'B'; they produce an
instance of the bytes type instead of the str type.
SPECIAL LITERAL - None
• None literal is used to indicate absence of value.
• Also indicate end of lists in python.
• None is used to specify to that field that is not created.
• None is not the same as 0, False, or an empty string. None is a datatype of its own
(NoneType) and only None can be None.
val1=10
val2=None
print(val1)
print(val2)
• Output
10
None
BAREBONES OF A PYTHON
PROGRAM
• Expressions.
• Statements.
• Comments.( inline comments and multiline comments or docstrings)
• Blocks and Indentation.
• Functions.
EXPRESSIONS
• A variable is defined only when you assign some value to it. Using an
undefined variable in a statement causes an error called name error.
Print(x). - will throw an error because x is undefined.
Find the output:
• X=20 p,q=3,5
• Print(x) q,r = p-2,p+2
print(p,q,r)
• A=b=c=0. this statement assigns value 0 to a, b and c. x=10
• x,y,z=10,20,30. This will assign the values orderwise.i.e. x=10, y=20 y,y=x+2,x+5
and z=30. print(y)
x,x =20,30
• x,y = y,x. the values are swapped x is having y value and y is having x y,y=x+10,x+20
value. print(x,y)
• While assigning values through multiple assigments, python first
evaluates the RHS expression and then assigns to LHS.
• a,b,c = 5,10,7
• b,c,a = a+1,b+2,c-1
• Print(a,b,c)
• Output : 6,6,12
DYNAMIC TYPING