Python Basics
Python Basics
Python Basics
Python Output
In [1]: # Python is case sensitive
print('Hello World')
Hello World
In [50]: print(hey) # it throws error beacuse only string is always in " "
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9556\1311815035.py in <module>
----> 1 print(hey) # it throws error beacuse only string always in " "
In [3]: print(7)
In [4]: print(True)
True
In [5]: # how print function strng in python -- it print all the values which we want to pr
print('Hello',3,4,5,True)
Hello 3 4 5 True
In [6]: # sep
print('Hello',3,4,5,True,sep='/') # sep is separator where in print function space
Hello/3/4/5/True
In [7]: print('hello')
print('world')
hello
world
In [8]: # end
print('you',end="=")
print ('me')
you=me
Data Types
1. Integers
In [9]: print(8)
2. Float (Decimal)
In [10]: print(8.44)
localhost:8888/nbconvert/html/Downloads/Pythoon 100 Days/100_Days_OF_Python/Day 1 - Basics OF Python.ipynb?download=false 1/7
9/20/23, 10:21 AM Day 1 - Basics OF Python
8.44
3. Boolean
In [11]: print(True)
print(False)
True
False
4. String (Text)
In [12]: print('Hello gem')
Hello gem
5. Complex
In [13]: print(5+6j)
(5+6j)
6. List
In Python, a list is a data type used to store a collection of values. It is one of the built-in
data types and is classified as a sequence type. Lists are ordered, mutable (which means you
can change their contents), and can contain elements of different data types, including
integers, floats, strings, or even other lists.
You can create a list in Python by enclosing a comma-separated sequence of values within
square brackets [ ]. For example:
In [ ]: print([1,2,3,4])
7. Tuple
In Python, a tuple is another data type used to store a collection of values, similar to a list.
However, there are some key differences between tuples and lists:
Immutable: The most significant difference is that tuples are immutable, meaning once
you create a tuple, you cannot change its contents (add, remove, or modify elements).
Lists, on the other hand, are mutable, and you can modify them after creation.
Tuples are often used when you have a collection of values that should not be changed
during the course of your program. For example, you might use tuples to represent
coordinates (x, y), dates (year, month, day), or other data where the individual components
should remain constant.
In [14]: print((1,2,3,4))
(1, 2, 3, 4)
8. sets
In Python, a set is a built-in data type used to store an unordered collection of unique
elements. Sets are defined by enclosing a comma-separated sequence of values within curly
braces {} or by using the built-in set() constructor. Sets automatically eliminate duplicate
values, ensuring that each element is unique within the set.
In [15]: print({1,2,3,4,5})
{1, 2, 3, 4, 5}
9. Dictionary
In Python, a dictionary is a built-in data type used to store a collection of key-value pairs.
Each key in a dictionary maps to a specific value, creating a relationship between them.
Dictionaries are also known as associative arrays or hash maps in other programming
languages.
In [16]: print({'name':'Nitish','gender':'Male','weight':70})
list
Out[18]:
In [19]: type({'age':20})
dict
Out[19]:
3. Variables
In Python, variables are used to store data values. These values can be numbers, strings,
lists, dictionaries, or any other data type. Variables are essential for manipulating and
working with data in your programs. Here's how you declare and use variables in Python:
1. Variable Names: Variable names (also known as identifiers) must adhere to the
following rules:
They can contain letters (a-z, A-Z), digits (0-9), and underscores (_).
They cannot start with a digit.
Variable names are case-sensitive, so myVar and myvar are treated as different
variables.
Python has reserved keywords (e.g., if, for, while, print) that cannot be used as variable
names.
1. Data Types: Python is dynamically typed, which means you don't need to declare the
data type of a variable explicitly. Python will determine the data type automatically
based on the assigned value.
1. Reassignment: You can change the value of a variable by assigning it a new value.
In [23]: x = 5
x = x + 1 # Updating the value of 'x' to 6
In [24]: print(x)
Multiple Assignment: Python allows you to assign multiple variables in a single line.
In [28]: print(a)
In [29]: a = 1
b = 2
c = 3
print(a,b,c)
1 2 3
In [30]: a=b=c= 5
print(a,b,c)
5 5 5
1. Single-line Comments: Single-line comments start with the hash symbol ( # ) and
continue until the end of the line. They are used to add comments on a single line.
1. Multi-line or Block Comments: Python does not have a specific syntax for multi-line
comments like some other languages (e.g., C, Java). However, you can create multi-line
comments by using triple-quotes ( ''' or """ ) as a string delimiter. These strings are
not assigned to any variable and are ignored by the interpreter. This is a common
practice for writing docstrings (documentation within functions and modules).
In [34]: '''
This is a multi-line comment.
It can span multiple lines.
'''
def my_function():
"""
This is a docstring.
It provides documentation for the function.
"""
pass
While these triple-quoted strings are not technically comments, they are often used as a
way to document code effectively.
Comments are crucial for making your code more understandable, both for yourself and for
others who may read your code. They help explain the purpose of variables, functions, and
complex algorithms, making it easier to maintain and debug code. Good commenting
practices can significantly improve code readability and maintainability.
4. User Input
How to get Input from the user in python?
Enter EmailSagar@95
'Sagar@95'
Out[35]:
print(result)
print(type(fnum))
5. Type Conversion
How to covert One Datatypeinto another In python?
int(): Converts a value to an integer data type. This is useful when you want to convert a
string or a floating-point number to an integer.
123
Out[44]:
In [45]: int_value = 42
float_value = float(int_value) # Converts the integer 42 to a float
float_value
42.0
Out[45]:
In [37]: a = 23
b = "24"
print(a+b)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9556\3112067906.py in <module>
2 b = "24"
3
----> 4 print(a+b)
It Gives error beacause We do not add float into integer datatyupe so we have to convert
above opeartion
In [38]: a = 23
b = "24"
print(float(a)+float(b))
47.0
6. Literals
localhost:8888/nbconvert/html/Downloads/Pythoon 100 Days/100_Days_OF_Python/Day 1 - Basics OF Python.ipynb?download=false 6/7
9/20/23, 10:21 AM Day 1 - Basics OF Python
In Python, literals are used to represent fixed values in your code. These values are not
variables or expressions but rather constants that have a specific value and data type
associated with them. Python supports various types of literals,
#Float Literal
float_1 = 10.5
float_2 = 1.5e2 # 1.5 * 10^2
float_3 = 1.5e-3 # 1.5 * 10^-3
#Complex Literal
x = 3.14j
print(a, b, c, d)
print(float_1, float_2,float_3)
print(x, x.imag, x.real)
In [48]: # binary
x = 3.14j
print(x.imag)
3.14
print(string)
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
This is Python
This is Python
C
This is a multiline string with more than one line code.
😀😆🤣
raw \n string
Operators In Python
1.Arithmetic Operators:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Floor Division (//)
Modulus (%)
Exponentiation (**)
print(5//2) # Floor Division -> It trasform into integer number= 2.5 convert into 2
11
-1
30
2.5
2
1
25
In [5]: print(4==4)
print(4!=4)
print(4<5)
print(4>5)
print(4<=4)
print(4>=4)
True
False
True
False
True
True
2. Logical Operators:
Logical AND (and)
Logical OR (or)
Logical NOT (not)
In [7]: p = True
q = False
False
True
False
3. Assignment Operators:
Assignment (=)
Add and Assign (+=)
Subtract and Assign (-=)
Multiply and Assign (*=)
Divide and Assign (/=)
Floor Divide and Assign (//=)
Modulus and Assign (%=)
Exponentiate and Assign (**=)
In [12]: x = 10
x += 5
print(x) # Equivalent to x = x + 5
15
In [16]: x = 10
x -= 3 # Equivalent to x = x - 3
x *= 2 # Equivalent to x = x * 2
x /= 4 # Equivalent to x = x / 4
x //= 2 # Equivalent to x = x // 2
x %= 3 # Equivalent to x = x % 3
x **= 2 # Equivalent to x = x ** 2
4.Bitwise Operators:
Bitwise AND (&)
Bitwise OR (|)
Bitwise XOR (^)
Bitwise NOT (~)
Left Shift (<<)
Right Shift (>>)
1
7
6
-6
10
2
These are the basic operators in Python. You can use them to perform various operations on
variables and values in your Python programs.
Immutable: Strings in Python are immutable, meaning once you create a string, you
cannot change its content. You can create new strings based on the original string, but
the original string remains unchanged.
1. Creating Strings
In [ ]: # create string with the help of single quotes
s = 'Hello World'
print(s)
Hello World
Hello World
-> In Python, you can create strings using both single quotes (') and double quotes ("), and
they are functionally equivalent. The choice between using single or double quotes mainly
depends on your personal preference or specific coding style guidelines.
If your string contains a quotation mark character (either a single quote or double quote),
you can use the other type of quote to define the string without escaping the inner quotes.
This can make your code more readable:
He said, "Hello!"
He said, 'Hello!'
In [ ]: # multiline strings
s = '''hello'''
s = """hello"""
s = str('hello')
print(s)
hello
Indexing
Indexing in Python refers to the process of accessing individual elements or characters
within a sequence, such as a string. Strings in Python are sequences of characters, and you
can access specific characters in a string using indexing. Indexing is done using square
brackets [ ], and Python uses a zero-based indexing system, meaning the first element has
an index of 0, the second element has an index of 1, and so on.
1. Positive indexing
2. Negative Indexing
In [ ]: s = "Hello World"
H
e
In [ ]: # Accessing individual characters using negative indexing (counting from the end)
print(s[-1]) # Accesses the last character, 'd'
print(s[-2]) # Accesses the second-to-last character, 'l'
d
l
Slicing
In Python, slicing is a technique used to extract a portion of a string, list, or any other
sequence-like data structure. When it comes to strings, slicing allows you to create a new
string by specifying a range of indices to extract a substring from the original string.
In [ ]: # string[start:end]
In [ ]: s = 'hello world'
print(s[1:5]) # from 1 index that is e to 4 index=o
print(s[:6]) # from 0 to 5
print(s[1:]) # from 1 to all
print(s[:]) # all string
print(s[-5:-1]) # with negative slicing
ello
hello
ello world
hello world
worl
wol
dlrow olleh
In [ ]: s = 'hello world'
print(s[-1:-6:-1])
dlrow
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_4564\2237226474.py in <module>
1 s = 'hello world'
----> 2 s[0] = 'H'
3
4 # Python strings are immutable
Immutable: Strings in Python are immutable, meaning once you create a string, you cannot
change its content. You can create new strings based on the original string, but the original
string remains unchanged.
operations on strings
In Python, strings are sequences of characters and are very versatile. You can perform a wide
range of operations on strings, including but not limited to:
1. Concatenation
You can concatenate (combine) two or more strings using the '+' operator:
# if we want space
print(a + " " + b)
Helloworld
Hello world
delhidelhidelhidelhidelhidelhidelhidelhidelhidelhidelhidelhidelhidelhi
In [ ]: print("*"*50)
**************************************************
In [ ]: s = "Hello world"
'l'
Out[ ]:
In [ ]: s[4:9]
'o wor'
Out[ ]:
3. String Methods
Python provides many built-in string methods for common operations like converting to
uppercase, lowercase, finding substrings, replacing, and more
Upper
file:///C:/Users/disha/Downloads/Day6 - Strings in Python part 2.html 1/3
9/25/23, 10:26 AM Day6 - Strings in Python part 2
HELLO, WORLD!
Lower
hello, world!
find
replace
Hi, World!
4. String Formatting
You can format strings using f-strings or the str.format() method
In [ ]: name = 'nitish'
gender = 'male'
4. String Length
You can find the length of a string using the len() function
13
5. Strip
In [ ]: 'hey '.strip() # it drop the Unwanted space prenet
'hey'
Out[ ]:
In [ ]:
Problems on Strings
1 . Find the length of a given string without using the len() function
counter = 0
for i in s:
counter += 1
print(s)
print('lenght of string is',counter)
Dishant
lenght of string is 7
pos = s.index('@')
print(s)
print(s[0:pos])
xyz123@gmail.com
xyz123
Eg 'hello how are you' is the string, the frequency of h in this string is 2.
counter = 0
for i in s:
if i == term:
counter += 1
print('frequency',counter)
frequency 1
result = ''
for i in s:
if i != term:
result = result + i
print(result)
5. Write a program that can check whether a given string is palindrome or not.
abba
malayalam
if flag:
print('Palindrome')
abba
Palindrome
if i != ' ':
temp = temp + i
else:
L.append(temp)
temp = ''
L.append(temp)
print(L)
7. Write a python program to convert a string to title case without using the title()
L = []
for i in s.split():
L.append(i[0].upper() + i[1:].lower())
print(" ".join(L))
digits = '0123456789'
result = ''
while number != 0:
result = digits[number % 10] + result
number = number//10
print(result)
print(type(result))
143243
143243
<class 'str'>
In [ ]:
Alt text
Characterstics of a List:
Ordered
Changeble/Mutable
Hetrogeneous
Can have duplicates
are dynamic
can be nested
items can be accessed
can contain any kind of objects in python
Creating a List
In [ ]: # Empty
print([])
# 1D -> Homo
print([1,2,3,4,5])
# 2D
print([1,2,3,[4,5]])
# 3D
print([[[1,2],[3,4]]])
# Heterogenous
print([1,True,5.6,5+6j,'Hello'])
[]
[1, 2, 3, 4, 5]
[1, 2, 3, [4, 5]]
[[[1, 2], [3, 4]]]
[1, True, 5.6, (5+6j), 'Hello']
['h', 'e', 'l', 'l', 'o']
Indexing
Slicing
In [ ]: # indexing
L = [[[1,2],[3,4]],[[5,6],[7,8]]]
L1 = [1,2,3,4]
# Positive Indexing
print(L1[1:4])
print(L[0][0][1]) # for 2
#How to extract 6
print(L[1][0][1])
[2, 3, 4]
[]
2
6
In [ ]: # Negative indexing
L = [[[1,2],[3,4]],[[5,6],[7,8]]]
L1 = [1,2,3,4]
print(L[-1])
In [ ]: # Slicing
L = [1,2,3,4,5,6]
print(L[::-1])
[6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, True]
In [ ]: # Extend -> The extend method is used to append elements from an iterable (e.g., an
L = [1,2,3,4,5]
L.extend([2])
L
[1, 2, 3, 4, 5, 2]
Out[ ]:
In [ ]: # insert -> The insert method allows you to add an item at a specific position in t
l = [1,2,3,4]
l.insert(1,100)
print(l)
[1, 2, 3, 4, 5, 300]
[1, 200, 300, 400, 5, 300]
#indexing
del l[2]
print(l)
# slicing
del l[2:4]
print(l)
[1, 2, 4, 5]
[1, 2]
In [ ]: # remove -> The remove method is used to remove the first occurrence of a specific
l = [1,2,3]
l.remove(2)
print(l)
[1, 3]
In [ ]: # pop -> The pop method is used to remove and return an item from the list based on
# If you don't provide an index, it will remove and return the last item by default
L = [1,2,3,4,5]
L.pop()
print(L)
[1, 2, 3, 4]
In [ ]: # clear -> The clear method is used to remove all items from the list, effectively
L = [1,2,3,4,5]
L.clear()
print(L)
[]
In [ ]:
In [ ]:
In [ ]:
Operations on Lists:
There are three types of Opeartion in List:
1. Arithmatic
2. Membership
3. Loop
L1 = [1,2,3,4,5]
L2 = [9,8,7,6,5]
# concatenation/Merge
print(L1 + L2)
print(L1*3)
print(L2*4)
[1, 2, 3, 4, 5, 9, 8, 7, 6, 5]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
[9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5]
In [ ]: # membership
L1 = [1,2,3,4,5]
L2 = [1,2,3,4,[5,6]]
False
True
In [ ]: # loops
L1 = [1,2,3,4,5]
L2 = [1,2,3,4,[5,6]]
L3 = [[[1,2],[3,4]],[[5,6],[7,8]]]
for i in L2:
print(i)
1
2
3
4
[5, 6]
List Functions
In [ ]: # len/min/max/sorted
L = [2,1,5,7,0]
print(len(L))
5
7
0
[0, 1, 2, 5, 7]
In [ ]: # count
l = [1,2,3,456,67867]
l.count(456)
1
Out[ ]:
In [ ]: # index
l = [3,5,7,9,3,23]
l.index(5)
1
Out[ ]:
In [ ]: # reverse
l = [1,2,3,4,6,78]
l.reverse()
print(l)
[78, 6, 4, 3, 2, 1]
In [ ]: # sort vs sorted
L = [2,1,5,7,0]
print(L)
print(sorted(L))
print(L)
L.sort()
print(L)
[2, 1, 5, 7, 0]
[0, 1, 2, 5, 7]
[2, 1, 5, 7, 0]
[0, 1, 2, 5, 7]
If you want to sort a list in-place, you should use the sort method. If you want to create a
new sorted list without modifying the original list, you should use the sorted function
List Comprehension
List Comprehension provides a concise way of creating lists.
print(L)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [ ]: # By List Comprehension
L = [i for i in range(1,11)]
print(L)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
L = [v*i for i in c]
print(L)
In [ ]: # Add sqaures
L = [2,3,4,5,6]
[i**2 for i in L]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Out[ ]:
In [ ]: a = [1,2,3]
b = a.copy()
print(a)
print(b)
a.append(4)
print(a)
print(b)
In [ ]:
Tuples In Python
A tuple in Python is an immutable, ordered collection of elements. It is similar to a list in that
it can store a variety of data types, including numbers, strings, and other objects, but unlike
lists, tuples cannot be modified once they are created.
This immutability means that you can't add, remove, or change elements within a tuple after
it's been defined.
Characterstics
Ordered
Unchangeble
Allows duplicate
Creating Tuples
In [ ]: # How to create empty tuple
t1 =()
print(t1)
# homo
t3 = (1,2,3,4,5)
print(t3)
# hetero
t4 = (1,2.5,True,[1,2,3])
print(t4)
# tuple
t5 = (1,2,3,(4,5))
print(t5)
()
('hello',)
<class 'tuple'>
(1, 2, 3, 4, 5)
(1, 2.5, True, [1, 2, 3])
(1, 2, 3, (4, 5))
('h', 'e', 'l', 'l', 'o')
In [ ]: print(t3)
(1, 2, 3, 4, 5)
# extract 3,4,5
print(t3[2:])
4
(3, 4, 5)
In [ ]: print(t5)
t5[-1][0]
In [ ]: print(t5)
del t5[-1]
Operation on Tuples
In [ ]: # + and *
t1 = (1,2,3,4)
t2 = (5,6,7,8)
print(t1 + t2)
# membership
print(1 in t1)
# iteration/loops
for i in t3:
print(i)
Tuple Functions
In [ ]: # len/sum/min/max/sorted
t = (1,2,3,4,5)
print(len(t))
print(sum(t))
print(min(t))
print(max(t))
print(sorted(t))
5
15
1
5
[1, 2, 3, 4, 5]
In [ ]: # count
t = (1,2,3,4,5)
t.count(3)
1
Out[ ]:
In [ ]: # index
t = (4,64567,3454,11,33,55)
t.index(64567)
1
Out[ ]:
Special Syntax
In [ ]: # tuple unpacking
a,b,c = (1,2,3)
print(a,b,c)
1 2 3
In [ ]: a = 1
b = 2
a,b = b,a
print(a,b)
2 1
In [ ]: a,b,*others = (1,2,3,4)
print(a,b)
file:///C:/Users/disha/Downloads/Day11 - Tuples In Python.html 3/5
9/30/23, 12:32 AM Day11 - Tuples In Python
print(others)
1 2
[3, 4]
In [ ]: # zipping tuples
a = (1,2,3,4)
b = (5,6,7,8)
tuple(zip(a,b))
1. Syntax:
Lists are mutable, which means you can change their elements after creation using
methods like append() , insert() , or direct assignment.
Tuples are immutable, which means once created, you cannot change their
elements. You would need to create a new tuple if you want to modify its contents.
3. Speed:
Lists are slightly slower than tuples in terms of performance because they are
mutable. Modifying a list may involve resizing or copying elements, which can
introduce overhead.
Tuples, being immutable, are generally faster for accessing elements because they
are more memory-efficient and do not require resizing or copying.
4. Memory:
Lists consume more memory than tuples due to their mutability. Lists require extra
memory to accommodate potential resizing and other internal bookkeeping.
Tuples are more memory-efficient since they don't have the overhead associated
with mutable data structures.
5. Built-in Functionality:
Both lists and tuples have common operations like indexing, slicing, and iteration.
Lists offer more built-in methods for manipulation, such as append() ,
insert() , remove() , and extend() . Lists are better suited for situations
where you need to add or remove elements frequently.
Tuples have a more limited set of operations due to their immutability but offer
security against accidental modifications.
6. Error-Prone:
Lists are typically used when you need a collection of items that can change over
time. They are suitable for situations where you want to add, remove, or modify
elements.
Tuples are used when you want to create a collection of items that should not
change during the program's execution. They provide safety and immutability.
In summary, the choice between lists and tuples depends on your specific needs. Use lists
when you require mutability and dynamic resizing, and use tuples when you want to ensure
immutability and need a more memory-efficient, error-resistant data structure.
Dictionary In Python
Dictionary in Python is a collection of keys values, used to store data values like a map,
which, unlike other data types which hold only a single value as an element.
Characterstics:
Mutable
Indexing has no meaning
keys can't be duplicated
keys can't be mutable items
Create Dictionary
In [ ]: # empty
d = {}
print(d)
#1D
d1 = {'name':'xyz', 'Age':23, 'gender':'male'}
print(d1)
# 2D dictionary
s = {
'name':'ramesh',
'college':'bit',
'sem':4,
'subjects':{
'dsa':50,
'maths':67,
'english':34
}
}
print(s)
Accessing Items
In [ ]: my_dict = {'name': 'Jack', 'age': 26}
my_dict['name'] # you have to write keys
'Jack'
Out[ ]:
In [ ]: d4['gender'] = 'male'
print(d4)
d4['weight'] = 70
print(d4)
# pop
d.pop(3) # it remove three
print(d)
#popitems
d.popitem() # it remove last item in the dictionary
print(d)
# del
del d['name']
print(d)
#clear
d.clear() # it clear dictionary
print(d)
In [ ]: s['subjects']['dsa'] = 80
s
{'name': 'ramesh',
Out[ ]:
'college': 'bit',
'sem': 4,
'subjects': {'dsa': 80, 'maths': 67, 'english': 34}}
Dictionary Operations
Membership
Iteration
In [ ]: print(s)
In [ ]: # membership
'name' in s
True
Out[ ]:
False
Out[ ]:
In [ ]: # ITERATION
for i in s:
print(i,s[i])
name ramesh
college bit
sem 4
subjects {'dsa': 80, 'maths': 67, 'english': 34}
Dictionary Functions
In [ ]: print(s)
In [ ]: # len/sorted
print(len(s))
sorted(s)
4
['college', 'name', 'sem', 'subjects']
Out[ ]:
In [ ]: # items/keys/values
print(s.items())
print(s.keys())
print(s.values())
In [ ]: # update
d1 = {1:2,3:4,4:5}
d2 = {4:7,6:8}
d1.update(d2)
print(d1)
{1: 2, 3: 4, 4: 7, 6: 8}
Dictionary Comprehension
In [ ]: # print 1st 10 numbers and their squares
{i:i**2 for i in range(1,11)}
In [ ]: distances = {'delhi':1000,'mumbai':2000,'bangalore':3000}
print(distances.items())
In [ ]: # using zip
days = ["Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
temp_C = [30.5,32.6,31.8,33.4,29.8,30.2,29.9]
{'Sunday': 30.5,
Out[ ]:
'Monday': 32.6,
'Tuesday': 31.8,
'Wednesday': 33.4,
'Thursday': 29.8,
'Friday': 30.2,
'Saturday': 29.9}
In [ ]: # using if condition
products = {'phone':10,'laptop':0,'charger':32,'tablet':0}
In [ ]: # Nested Comprehension
# print tables of number from 2 to 4
{i:{j:i*j for j in range(1,11)} for i in range(2,5)}