0% found this document useful (0 votes)
3 views7 pages

Telesko Python 2

This document covers key concepts of dictionaries in Python, including their properties, methods for accessing values, and how to create and manipulate them. It also discusses variables, memory management, garbage collection, and various built-in data types in Python such as NoneType, numbers, booleans, lists, tuples, sets, strings, ranges, and dictionaries. Additionally, it provides code examples to illustrate these concepts.

Uploaded by

ajk952626
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Telesko Python 2

This document covers key concepts of dictionaries in Python, including their properties, methods for accessing values, and how to create and manipulate them. It also discusses variables, memory management, garbage collection, and various built-in data types in Python such as NoneType, numbers, booleans, lists, tuples, sets, strings, ranges, and dictionaries. Additionally, it provides code examples to illustrate these concepts.

Uploaded by

ajk952626
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Dictionary

In this lecture we are discussing about Dictionary:


-- if you want to accessing the data by using key then we are using dictionary
-- Dictionary uses key and value pair
key and value
in python
data={1:'Navin',2:'kiran',4:'Harsh'} # 1 is key and Navin is value and 2 is key and kiran is value
and 4 is key and Harsh is value

property of dictionary:
-- key must be immutable and unique
immutable means we can't change the value
unique means we can't repeat the key
-- value can be anything
-- we can access the value by using key
-- we can't access the value by using index
-- we can't access the value by using value

fetching the value from dictionary


data[4] # we can access the value by using key here key is 4
data[3] # you get error because key is not present in dictionary

get() method:
-- we can access the value by using get() method
-- if key is present in dictionary then it will return the value
-- if key is not present in dictionary then it will return None
data.get(1)
data.get(3) # not get anything
print(data.get(3)) # it will return None
data.get(1,'Not Found')
data.get(3,'Not Found')

use of zip() method:


-- we can combine the two list by using zip() method
-- it will return the tuple
keys = ['Navin','Kiran','Harsh']
values=['Python','Java','JS']

use of dict() method:


-- we can convert the tuple into dictionary by using dict() method
data=dict(zip(keys,values))
data['Kiran']
data['Monika'] # it will give error because key is not present in dictionary

add the value in dictionary:


data['Monika']='CS'
data

delete the value from dictionary:

telesko python Page 1


delete the value from dictionary:
del data['Harsh']

Nested Dictionary:
-- we can store the dictionary inside the dictionary
prog={'JS':'Atom','CS':'VS','Python':['Pycharm','Sublime'],'Java':{'JSE':'Netbeans','JEE':'Eclipse'
}}
prog
prog['JS']
prog['python'][1]
prog['Java']
prog['Java']['JEE']

telesko python Page 2


More on Variables

In this lecture we will learn:


- Variables in Python
- Memory area of variables in Python
- How to get the address of a variable?
- Garbage collection
- Data types in Python

#1
- Every variable has its address.
- In Python, the id() function is used to get the address of a variable.
- We can also assign the value of one variable to any other variable.
- In python, whenever you create multiple variables and if they have the same
data then they will point towards the same box or same memory area.
- Everything is an object in Python.
- Variables are also known as tags as we tag the value with a variable.
- If the same variable store multiple values, then that variable will point towards
the new memory area where the new value is get stored.

#2
- If there is any data present in the memory that is not referenced by any
variable, then that will be removed from the memory by the Garbage collector.

#3
- The value of variables can be changed but the value of the constant remains
the same.
- In python, we represent constants through capital letters.
- type() function is used to get the data type of value of a variable.
- Besides in-built data types, we can also create our own types.

Code

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit


(AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> num = 5
>>> id(num)
140731200725776
>>> name = 'navin'
>>> id(name)
2243898937160
>>> a = 10
>>> b = a
>>> a
10
>>> b
10
>>> id(a)
140731200725936
>>> id(b)

telesko python Page 3


>>> id(b)
140731200725936
>>> id(10)
140731200725936
>>> k = 10
>>> id(k)
140731200725936
>>> a = 9
>>> id(a)
140731200725904
>>> id(b)
140731200725936
>>> k = a
>>> id(k)
140731200725904
>>> b = 8
>>> PI = 3.14
>>> PI
3.14
>>> PI = 3.15
>>> type(PI)
<class 'float'>
>>>

telesko python Page 4


Data Types
In this lecture we are discussing about DataTypes in Python:
-- why it is important?
-- how to use it ?

Python has several built-in data types. Here are some of the most common ones:

i) NoneType: This is a special data type that represents the absence of a value. It is similar to
null in other languages.
ii) Numbers: These can be integers, floating-point numbers, or complex numbers.
iii) Booleans: These are values that represent True or False.
iv) Lists: These are ordered collections of objects, enclosed in square brackets.
v) Tuples: These are similar to lists, but are immutable (i.e., their contents cannot be
changed), and are enclosed in parentheses.
vi) Sets: These are unordered collections of unique elements, enclosed in curly braces.
vii) Strings: These are sequences of characters, enclosed in single or double quotes.
viii) Ranges: These are immutable sequences of numbers, and are commonly used to iterate
over a sequence of numbers in a for loop.
ix) Dictionaries: These are collections of key-value pairs, enclosed in curly braces.

i)None Type
a=None
type(a)

ii)Numbers
int: if you want to assign a integer value to a variable
a=5
type(a)

float: if you want to assign a float value to a variable


num =2.5
type(num)

complex: if you want to assign a complex value to a variable


num =2+9j
type(num)

type conversion: if you want to convert one data type to another data type
a=5.6
b=int(a)
type(b) # output : int
k=float(b)
type(k) # output : float
c=complex(4,5)
type(c) # output : complex

iii)boolean: if you want to assign a variable with a boolean value


a= True
type(a) # output : bool
bool=3 less then5
telesko python Page 5
bool=3 less then5
True
type(bool)

Sequence data types : if you want to assign a variable with multiple values
List, Tuple, Set, String, Range.

iv) List if you want to assign a variable with multiple values and you want to change the
values
-- In Python, a list is a collection of ordered and mutable elements enclosed
in square brackets. Lists are one of the most commonly used data structures in
Python because of their versatility and flexibility.

lst=[25,36,45,12]
type(lst) # output : list

v) Tuple: if you want to assign a variable with multiple values and you donot want to change
the values make immutable
-- In Python, a tuple is a collection of ordered and immutable elements enclosed in
parentheses.
Tuples are similar to lists, but they cannot be modified once they are created, which makes
them
useful for storing data that should not be changed during the program's execution.

t=(25,36,45,12,7)
type(t) # output : tuple

vi) Set: if you want to assign a variable with multiple values and you donot want to change
the values and you donot want to duplicate values
-- In Python, a set is an unordered collection of unique elements enclosed in curly braces.
Sets are useful for storing data that should not contain duplicates, such as a list of
users on a website.

s={25,36,45,12,25,36}
type(s) # output : set
#output: {36, 12, 45, 25}

vii) String: if you want to assign sequence of characters to a variable


-- In Python, a string is a sequence of characters enclosed in single or double quotes.
Strings are immutable, which means that they cannot be modified once they are created.

str = "hello"
type(str) # output : str

we are not talk about char data type in python


st='a' # every character is a string in python

viii) Range: if you want to assign a variable with multiple values and you don't want to
change the values and you want to generate a sequence of numbers
-- In Python, a range is a sequence of numbers that is immutable and iterable.
Ranges are commonly used to iterate over a sequence of numbers in a for loop.

telesko python Page 6


Ranges are commonly used to iterate over a sequence of numbers in a for loop.

range(10) # range data type


type(range(10)) # output : range
list(range(2,10,2)) # output : [2, 4, 6, 8]

ix) Dictionary: if you want to assign a variable with multiple values and you donot want to
change the values and you want to assign a key to each value
-- In Python, a dictionary is a collection of key-value pairs enclosed in curly braces.
Dictionaries are useful for storing data that is associated with a key, such as a list of
users on a website and their corresponding email addresses.

d={1:'a',2:'b',3:'c'}
type(d)

d1={'navin':'samsung','rahul':'iphone','kiran':'oneplus'}
d1.values() # output : dict_values(['samsung', 'iphone', 'oneplus'])
d1.keys() # output : dict_keys(['navin', 'rahul', 'kiran'])
d['rahul'] #output : 'iphone'
d1.get('kiran') #output : 'oneplus'

telesko python Page 7

You might also like