-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.Variables_DataTypes.py
More file actions
206 lines (120 loc) · 4.1 KB
/
6.Variables_DataTypes.py
File metadata and controls
206 lines (120 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# ==========================
# Variables and Data Types
# ==========================
# immutable data type
a = 10
print(a)
print(type(a)) #Output: <class 'int'>
# immutable data type
b = 3.14
print(b)
print(type(b)) #Output: <class 'float'>
# immutable data type
c = "Hello, World!"
print(c)
print(type(c)) #Output: <class 'str'>
# Boolean type (immutable)
d = True
print(d)
print(type(d)) #Output: <class 'bool'>
# Ordered collection of values (mutable)
e = [1, 2, 3, 4, 5]
print(e)
print(type(e)) #Output: <class 'list'>
# Immutable sequence of values
f = (1, 2, 3)
print(f)
print(type(f)) #Output: <class 'tuple'>
# Key-value pairs (mutable)
g = { "name": "Alice", "age": 30 }
print(g)
print(type(g)) #Output: <class 'dict'>
# Unordered collection of unique values (mutable)
h = {1, 2, 3, 4, 5}
print(h)
print(type(h)) #Output: <class 'set'>
i = None
print(i)
print(type(i)) #Output: <class 'NoneType'>
# used to represent complex numbers like 2 + 3j to use in mathematical calculations (immutable)
j = complex(2, 3)
print(j)
print(type(j)) #Output: <class 'complex'>
# 1️⃣ Mutable Data Types
# Can be changed after creation (you can modify, add, or remove elements).
# Examples:
# list → [1, 2, 3] → you can do list.append(4) or list[0] = 10
# dict → {"a":1} → you can do dict["b"] = 2
# set → {1, 2, 3} → you can do set.add(4)
# 2️⃣ Immutable Data Types
# Cannot be changed after creation (any modification creates a new object).
# Examples:
# int → x = 5 → x + 1 creates a new integer; original x stays 5
# float → y = 3.14 → cannot change value in-place
# str → "hello" → "hello"[0] = "H" is not allowed
# tuple → (1, 2, 3) → cannot change elements
# frozenset → immutable version of a set
# complex → complex numbers like 2 + 3j are immutable
# ==========================
# Type Conversion
# ==========================
x = 5 # int
y = 2.5 # float
z = "10" # str
print(type(x), type(y), type(z)) #Output: <class 'int'> <class 'float'> <class 'str'>
# Converting int to float
x_float = float(x)
print(x_float) #Output: 5.0
print(type(x_float)) #Output: <class 'float'>
# Converting float to int
y_int = int(y)
print(y_int) #Output: 2
print(type(y_int)) #Output: <class 'int'>
# Converting str to int
z_int = int(z)
print(z_int) #Output: 10
print(type(z_int)) #Output: <class 'int'>
# Converting str to float
z_float = float(z)
print(z_float) #Output: 10.0
print(type(z_float)) #Output: <class 'float'>
# Converting int to str
x_str = str(x)
print(x_str) #Output: "5"
print(type(x_str)) #Output: <class 'str'>
# =========================================
# Every thing in Python is an Object
# =========================================
# https://chatgpt.com/s/t_6922f8dcd0d88191abcf288a4312ce8a
# What “Everything is an object” means in Python
# In Python:
# An object is anything that stores data and can do things.
# Everything you use in Python is one of these objects — numbers, text, lists, functions, even your own programs are objects.
# 1. Objects have three things
# Identity: Who the object is (like its ID).
# a = 5
# print(id(a)) # prints a unique number identifying this object
# Type: What kind of object it is.
# a = 5
# print(type(a)) # <class 'int'> → it's an integer object
# Value: What it actually holds.
# a = 5
# print(a) # 5
# 2. Objects can do things (Methods)
# Each object can have actions (functions attached to it) called methods.
# Example: a string object "hello" can do .upper(), .replace(), etc.
# name = "jackal"
# print(name.upper()) # JACKAL
# 3. Even functions, classes, and modules are objects
# Functions can be stored in variables:
# def greet():
# print("Hi!")
# f = greet # assign function to variable
# f() # prints "Hi!"
# Classes are objects too, which is why you can create classes dynamically.
# 4. Why it matters
# Python treats everything uniformly.
# This makes the language flexible — you can:
# Pass functions as arguments
# Store objects in lists or dictionaries
# Add attributes to objects dynamically