Skip to content

Commit b025f61

Browse files
committed
2024/03/14 - Adding variable and data type demo python code
0 parents  commit b025f61

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

concepts/DataTypeDemo.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# This file gives demo about different data types
2+
3+
# Numeric data type
4+
# Int, Float, Complex data type
5+
6+
def _numeric_data_type_demo() -> None:
7+
print("Inside _numeric_data_type_demo")
8+
a = 10
9+
b = 10.5
10+
c = 1 + 2.3j
11+
12+
print("Data type of a ", type(a), " and valud of a ", a)
13+
print("Data type of b ", type(b), " and valud of b ", b)
14+
print("Data type of c ", type(c), " and valud of c ", c)
15+
print("c.real ", c.real, " c.imag", c.imag)
16+
print("Adding 1 to c.real and adding 2 to imag part of c")
17+
c = c.__add__(1 + 2j)
18+
print("Value of c post changes ", c)
19+
print("**" * 25)
20+
21+
22+
_numeric_data_type_demo()
23+
24+
25+
# Sequence data type
26+
# String, List, tuple type
27+
28+
def _string_data_type_demo() -> None:
29+
print("Inside _string_data_type_demo")
30+
_my_str = 'Hello'
31+
_my_str1 = " World"
32+
_my_str2 = """!!!"""
33+
34+
# concat strings
35+
_final_str = _my_str + _my_str1 + _my_str2
36+
print("Final string ", _final_str)
37+
38+
# slice operation, here string index will start from 0 and traverse till 5-1 index position i.e. 4
39+
print("_final_str[0:5] ", _final_str[0:5])
40+
41+
# to reverse string
42+
print("Reverse string ", _final_str[::-1])
43+
44+
# update string at specific position
45+
# strings are immutable hence they can modify using below way only
46+
_new_str = "RM"
47+
print("Before update char at 2nd position : ", _new_str, " and id : ", id(_new_str))
48+
_new_str = _new_str[0:1] + "A" + _new_str[-1]
49+
print("Post update char at 2nd position : ", _new_str, " and id : ", id(_new_str))
50+
51+
# Repetition factor
52+
print("**" * 25)
53+
54+
55+
_string_data_type_demo()
56+
57+
58+
def _list_data_type_demo() -> None:
59+
print("Inside _list_data_type_demo")
60+
_my_list = [1, "Rohit", 2, "Pooja"]
61+
print("List : ", _my_list)
62+
63+
# slice operation
64+
print("List slice [0:2] : ", _my_list[:2])
65+
66+
# to reverse list
67+
print("Reverse list ", _my_list[::-1])
68+
69+
# concat with another list
70+
print("Another List : ", [3, "Ram"])
71+
_my_list = _my_list + [3, "Ram"]
72+
print("Concat List : ", _my_list)
73+
74+
# add element at specific position
75+
_my_list.insert(2, 4)
76+
print("Inserting element 4 at 2nd position ", _my_list)
77+
78+
# removing element from specific position
79+
_my_list.pop(2)
80+
print("Removing element from 2nd position ", _my_list)
81+
82+
print("**" * 25)
83+
84+
85+
_list_data_type_demo()
86+
87+
88+
def _tuple_data_type_demo() -> None:
89+
print("Inside _tuple_data_type_demo")
90+
_my_tuple = (1, "Rohit", 31, 25000)
91+
print("_my_tuple : ", _my_tuple)
92+
93+
# slice
94+
print("Tuple slice [0:2] : ", _my_tuple[:2])
95+
96+
# get element at 3rd position
97+
print("get element at 4th position : ", _my_tuple[3])
98+
99+
# iterate through tuple
100+
print("Iterate through tuple")
101+
for i in _my_tuple:
102+
print(i)
103+
104+
print("**" * 25)
105+
106+
107+
_tuple_data_type_demo()
108+
109+
110+
def _dictionary_data_type_demo() -> None:
111+
_my_map = {1: "Rohit", 2: "Pooja", 5: "Virat"}
112+
print("_my_map", _my_map)
113+
114+
print("Get value of key 5 : ", _my_map[5])
115+
116+
print("Get all keys : ", _my_map.keys())
117+
print("Get all values : ", _my_map.values())
118+
119+
# check if key is present or not
120+
print("Check key 3 present or not : ", _my_map.__contains__(3))
121+
122+
_my_map.pop(2)
123+
print("Post Removal of key 2 : ", _my_map)
124+
125+
_my_map.update({1: "Pooja"})
126+
print("Post updating value of key 1 : ", _my_map)
127+
128+
print("**" * 25)
129+
130+
131+
_dictionary_data_type_demo()
132+
133+
134+
def _set_data_type_demo() -> None:
135+
set1 = {1, 1, "Rohit"}
136+
print("set1 ", set1)
137+
138+
print("**" * 25)
139+
140+
141+
_set_data_type_demo()

concepts/VariableDemo.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This file gives demo about different types of variables
2+
# and there usages
3+
4+
print("Welcome to python demo!!!")
5+
6+
7+
# local variable
8+
def _local_variable_demo() -> None:
9+
print("Inside _local_variable_demo")
10+
x = 10
11+
print("Value of x ", x)
12+
return None
13+
14+
15+
_local_variable_demo()
16+
17+
name = "Rohit"
18+
19+
20+
# global variable
21+
def _global_variable_demo() -> None:
22+
print("Inside _global_variable_demo")
23+
global name
24+
name += " Phadtare"
25+
print("Value of name ", name)
26+
return None
27+
28+
29+
_global_variable_demo()
30+
31+
32+
# non local variable
33+
def _non_local_variable_demo() -> None:
34+
print("Inside _non_local_variable_demo")
35+
num = 10
36+
print("value of nonlocal variable before calling _inside_func ", num)
37+
38+
def _inside_func() -> None:
39+
num1 = 20
40+
nonlocal num
41+
num += num1
42+
43+
_inside_func()
44+
print("value of nonlocal variable post execution of _inside_func ", num)
45+
46+
47+
_non_local_variable_demo()
48+
49+
# Object Identity demo
50+
a = 50
51+
b = a
52+
print("ID of a ", id(a))
53+
print("ID of b ", id(b))
54+
a = 100
55+
print("ID of a after assigning new value ", id(a))
56+

0 commit comments

Comments
 (0)