You are on page 1of 23

Content

◻ Literals in Python
◻ Build-in format() Function
◻ Escape Sequences
◻ String Formatting Function
◻ Comments and Constants in Python
◻ Variables in Python
◻ Identifier in Python
◻ Built-in Data Types
◻ Python Indentation
◻ Reserved Keywords in Python
◻ Operators in Python
Literals in Python
❑ Literal: Literal is a raw data given in a variable or
constant. In Python, there are various types of
literals they are as follows:

Numeric Literals:

Numeric Literals are immutable (unchangeable).


Numeric literals can belong to 4 different numerical
types Integer, long integers, Float, and Complex.
Examples : 10, 535353535353L, 10.5, (50+5j)
String literals
A string literal is a sequence of characters surrounded by quotes. We can use both
single, double quotes for a string. And, a character literal is a single character
surrounded by single or double quotes.
Example : >>> print(‘Hello’) # Using Single quotes
Hello

Example : >>> print(“Hello”) # Using Double quotes


Hello

◻Using Triple quotes : we can specify multi-line strings using triple quotes.
◻Example
‘ ‘ ‘ good morning everyone.
Welcome to python
Programming language. ‘ ‘ ‘
String literals

Try this code:

word = 'word‘
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""“

Print (word)
Print (sentence)
Print (pargraph)

str = “Hello World!”


print (str) # Prints complete string
print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th
print (str[2:]) # Prints string starting from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST") # Prints concatenated string
Build-in format() Function
◻ Any floating-point value may contain an arbitrary number of
decimal places, so it is always recommended to use the build-in
format() function to produce string version of a number with
specific number of decimal places.

◻ For Example:

◻ Without using build-in format() function


print(float(16/(float(3))))
5.33333333333333

◻ Using build-in format() function


print(format(float(16/(float(3))), '.2f'))
5.33
Escape Sequences
◻Some characters cannot be directly included in a
string. Such characters must be escaped by placing
a backslash before them.
Example: print('what's your name?')
SyntaxError: invalid syntax
Escape Seq. Purpose
\’ ‘
\” “
Example: print('what\'s your name?') \n new line
what's your name? \t tab

Example:
print("boy replies,\"my name is Vijay\"")
boy replies,"my name is Vijay"
String Formatting Function
◻ This function can be used to control the display of
strings.
Syntax: format(value, format_specifier)
◻ Value -> value or string to be displayed
◻ format_specifier -> formatting options

Example:
format('Hello', '<30')
'Hello '

Example:
format('Hello', '>30')
' Hello'
Comments and Constants in Python
❑ Creating a Comment
Comments starts with a #, and python will ignore them:
Example :
# this is a comment
print(“Hello”)

“ “ “this is a very long sentence


and it after two lines” ” ”

❑ Constant : A constant is a type of variable whose value cannot be


changed. It is helpful to think of constants as containers that hold
information which cannot be changed later.
Variables in Python
◻ Named labels, whose values can be used and processed
during program run, are called Variables.
◻ By assigning different data types to variables, you can
store integers, floating point or characters in these
variables.
◻ Examples :
a = 70
b = 100.0
name = “john”
Identifier in Python
Identifier is the name given to a variable, function, class
or object. All identifiers must obey the following rules.
An identifier:
1. Is a sequence of characters that consists of letter, digit
and underscore.
2. Can be of any length
3. Start with letter which can be lower case
4. Can start with underscore ‘_’
5. Can not start with a digit
6. Can not be a keyword
Example : _sum, person
Built-in Data Types
◻ Python has the following data types built-in by
default.
◻ Text Type : str
◻ Numeric Types : int, float, complex
◻ Sequence Types : list, tuple, range
◻ Mapping Type : dict
◻ Set Types : set, frozenset
◻ Boolean Type : bool
◻ Binary Types : bytes, bytearray, memoryview
Python Indentation
◻ Indentation refers to the spaces at the beginning of a code
line.
◻ Whereas in other programming languages the indentation in
code is for readability only, but the indentation in Python is
very important.
◻ Python uses indentation to indicate a block of code.

Example:
if 5 > 2:
print("Five is greater than two!")

Note: Python will give you an error if you skip the indentation.
Reserved Keywords in Python

◻ Keywords are reserved words with fixed meanings


assigned to them. Keywords can not be used as an
identifier or variable.

◻ Some of the examples of keywords in python are :-


and, or, not, if, else, elif, for, while, return etc.
Reserved Keywords in Python
Operators in Python
Types of Operators used in Python
◻ Arithmetic Operators (+, -, *, /, %, **, //)
◻ Comparison Operators (==, >, <, >=, <=)
❑ Assignment Operators (=, +=, -=, *=, /=)
◻ Logical Operators (AND, OR NOT)
◻ Bitwise Operators (&, |, ^, >>, <<, ~)
◻ Membership Operators (in, not in)
◻ Identity Operators (is, is not)
Arithmetic Operators
❑It is used for performing basic Arithmetic
Operation (+, -, *, /, %, **, //).
Example :
>>> a, b, c = 10, 5, 2
>>> print(“sum =”,(a+b))
sum = 15
Arithmetic Operators
Comparison Operators
◻ These operators (==, >, <, >=, <=) compare the
values on either sides of them and decide the
relation among them.
◻ Example :
>>> a, b = 10, 5
>>> print(“a>b is”,(a>b))
a>b is True
Assignment Operators
◻ Python provide various assignment operators
(=, +=, -=, *=, /=).
◻ Example :
>>> a, b = 10, 5
>>> a+=b
>>>Print(a)
15
Assignment Operators
Logical Operators
◻ There are three operators – and, or, not
◻ Example :
>>> a, b, c, d = 10,5,2,1
>>> print((a>b) and(c>d))
True
Home work
◻ Check the uses of all operators discussed, along
with bitwise operators, along with coding
◻ Calculate the area of a circle with radius 5.3 units
◻ Calculate the volume of a sphere with radius 6
units
◻ Assign values to a, b, c, and perform addition,
sub, multiplication, division among them and
print values/result of the operations
◻ Perform string display with large sentence /
paragraph
References

https://www.tutorialspoint.com/python/python_variable_types
.htm

For home work questions:


https://www.tutorialspoint.com/python/arithmetic_operators_
example.htm
https://www.tutorialspoint.com/python/assignment_operators
_example.htm

You might also like