Lecture 22 - Python Datatypes, Variables, Input Output Statements
Lecture 22 - Python Datatypes, Variables, Input Output Statements
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than
some other programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as
it is written. This means that prototyping can be very quick.
Int - Integer value can be any length such as integers 10, 2, 29, -20, -
150 etc. Python has no restriction on the length of an integer. Its value
belongs to int
c = 1+3j
print(c)
print("The type of c", type(c))
Numbers
print(123123123123123123123123123123123123123123123123 + 1)
Output
123123123123123123123123123123123123123123123124
Multiline Strings
List
Python Lists are similar to arrays in C.
The items stored in the list are separated with a comma (,) and
enclosed within square brackets [].
We can use slice [:] operators to access the data of the list.
Each key is separated from its value by a colon (:), the items are
separated by commas, and the whole thing is enclosed in curly
braces.
An empty dictionary without any items is written with just two curly
braces, like this: {}.
Keys are unique within a dictionary while values may not be. The
values of a dictionary can be of any type, but the keys must be of an
immutable data type such as strings, numbers, or tuples.
Dictionary
Pair1: ‘Name’:’Zara’
Key is Name and its value is Zara
Pair 2: 'Age': 7
Key is Age and its value is 7
Accessing Values in Dictionary
To access dictionary elements, you can use the familiar square brackets
along with the key to obtain its value.
OUTPUT
Boolean
Booleans represent one of two values: True or False.
You can evaluate any expression in Python, and get one of two
answers, True or False.
When you compare two values, the expression is evaluated and Python returns the
Boolean answer:
Example
print(10 > 9)
print(10 == 9)
print(10 < 9)
OUTPUT:
True
False
False
Boolean
Any list, tuple, set, and dictionary are True, except empty ones.
Input and Output in Python
Taking Input from the user
Syntax:
input('prompt')
where, prompt is a string that is displayed on the string at the time of taking input.
Example:
OUTPUT:
AMRITA
Taking Input from the user
Python Script
OUTPUT
Syntax
print(object(s), sep=separator, end=end, file=file, flush=flush)
Parameter Values
Parameter Description
object(s) Any object, and as many as you like. Will be converted to string before printed
sep='separator' Optional. Specify how to separate the objects, if there is more than one. Default is ' '
end='end' Optional. Specify what to print at the end. Default is '\n' (line feed)
flush Optional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False
Displaying Output
Python Script
OUTPUT
Python Comments
Comments can be used to make the code more readable.
Comments starts with a #, and Python will ignore them:
Example:
Python script
x=5
y = "John"
print(x)
print(y)
Output
5
John
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different
variables)
Variable Names
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
OUTPUT
Orange
Banana
Cherry
Assigning same value to multiple variables in one
line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
OUTPUT
Orange
Orange
Orange
Output Variables
Example:
x = "awesome"
print("Python is " + x)
Output:
Python is awesome
x=5
y = "John"
print(x + y)
Output:
TypeError: unsupported operand type(s) for +: 'int'
and 'str'
Python Casting
There may be times when you want to specify a type on to a variable.
This can be done with casting.
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators
Note:
Floor Division (//) –
The division of operands where the result is the quotient in which the digits after
the decimal point are removed. But if one of the operands is negative, the result
is floored, i.e., rounded away from zero (towards negative infinity) −
9//2 = 4
9.0//2.0 = 4.0,
-11//3 = -4
-11.0//3 = -4.0
•Arithmetic operators
Python Script OUTPUT
Python Assignment Operators
Python Comparison Operators
Python Script
x = 10
y = 12
print('x > y is',x>y)
print('x < y is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)
Python Identity Operators:
Explanation:
Membership operators are used to test whether a value or variable is found in a
sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
Python Membership Operators
Python Bitwise Operators
Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary
Arithmetic operations on strings
+ operator (for concatenation):
When you use + on strings, it will concatenate them together.
print('hello' + 'world')
Output:
helloworld
print('hello' * 3)
Output:
hellohellohello
Note:
If you place two strings next to each other, they will also be concatenated:
print('hello' 'world')
Output:
helloworld
Discussion
What is the maximum possible length of an
identifier?
a) 31 characters
b) 63 characters
c) 79 characters
d) none of the mentioned
Answer: d
Explanation: Identifiers can be of any length.
Which of the following is invalid?
a) _a = 1
b) __a = 1
c) __str__ = 1
d) none of the mentioned
Answer: d
What will be the output of the following program on
execution?
if False:
print ("inside if block")
elif True:
print ("inside elif block")
else:
print ("inside else block")
A. inside if block
B. inside elif block
C. inside else block
D. Error
Ans : B
What will be the output of following Python
code snippet?
str1="012"
num1=2 A. 7
num2=0 B. Infinite Loop
for i in range(4): C. 0
D. Error
num1+=2
for j in range(len(str1)): Ans : C
num2=num2+num1
num3=num2%int(str1)
print(num3)
What is the type of inf ?
a) Boolean
b) Integer
c) Float
d) Complex
Answer: c
a) x = 0b101
b) x = 0x4f5
c) x = 19023
d) x = 03964
Answer: d
a)1 2 3 4 5 6
b)1 2 3 4 5 6 7
c)Error
Answer: a
What will be the output of the following Python
code?
for i in range(2.0):
print(i)
Answer: c
for i in range(int(2.0)):
print(i)
Answer: b
Explanation: range(int(2.0)) is the same as range(2).