-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path2
More file actions
263 lines (185 loc) · 5.09 KB
/
2
File metadata and controls
263 lines (185 loc) · 5.09 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Day-2 | Python syntax, data types, and variables
## Python Syntax:
Python is known for its clean and readable syntax. Here are some fundamental syntax rules:
1. **Statements and Indentation**:
- Python uses indentation (whitespace) to define blocks of code, rather than curly braces `{}`. This is a unique feature of Python and helps in maintaining a clean and consistent code structure.
Example:
```python
if condition:
# This block is indented, it belongs to the if statement
print("This is indented")
else:
print("This is also indented")
```
2. **Comments**:
- Comments start with a `#` character and are ignored by the Python interpreter. They are used to add explanations or notes within the code.
Example:
```python
# This is a comment
```
3. **Variables and Identifiers**:
- Variables are used to store data. In Python, you can assign a value to a variable using `=`. Identifiers are names given to variables, functions, classes, etc.
Example:
```python
# Variable assignment
x = 10
name = "John"
# Identifiers
my_variable = 5
```
4. **Data Types**:
- Python has several built-in data types, including integers, floats, strings, booleans, lists, tuples, dictionaries, etc. We'll discuss them in more detail below.
## Data Types:
### 1. Numeric Types:
- **int**: Integer numbers (e.g., -5, 0, 100)
- **float**: Floating-point numbers (e.g., 3.14, -0.5)
Examples:
```python
num_int = 42
num_float = 3.14
```
### 2. String:
- A sequence of characters enclosed in single (' '), double (" "), or triple (''' ''' or """ """) quotes.
Examples:
```python
name = "Alice"
message = 'Hello, world!'
```
### 3. Boolean:
- Represents truth values `True` or `False`.
Examples:
```python
is_valid = True
has_permission = False
```
### 4. List:
- Ordered collection of items, which can be of different types.
Example:
```python
my_list = [1, 2, 3, "hello", True]
```
### 5. Tuple:
- Similar to lists but immutable (cannot be changed after creation).
Example:
```python
my_tuple = (1, 2, 3, "world")
```
### 6. Dictionary:
- Collection of key-value pairs.
Example:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
```
### 7. Set:
- Unordered collection of unique items.
Example:
```python
my_set = {1, 2, 3, 4, 4, 4} # Only contains 1, 2, 3, 4
```
## Examples:
Let's combine these concepts in some examples:
### Example 1: Variables and Basic Operations
```python
# Variables and basic operations
x = 5
y = 3
# Arithmetic operations
sum_result = x + y
difference_result = x - y
product_result = x * y
division_result = x / y
print(sum_result, difference_result, product_result, division_result)
```
### Lists:
#### Example 1 - Creating and Manipulating Lists:
```python
# Creating a list
my_list = [1, 2, 3, 4, 5]
# Accessing elements
print("First element:", my_list[0])
print("Last element:", my_list[-1])
# Modifying elements
my_list[2] = 10
print("Modified list:", my_list)
# Appending and removing elements
my_list.append(6)
my_list.remove(4)
print("Updated list:", my_list)
```
### Tuples:
#### Example 2 - Creating and Accessing Tuples:
```python
# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
# Accessing elements
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])
```
### Dictionaries:
#### Example 3 - Creating and Manipulating Dictionaries:
```python
# Creating a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Accessing values
print("Name:", my_dict['name'])
print("Age:", my_dict['age'])
# Adding a new key-value pair
my_dict['email'] = 'john@example.com'
print("Updated dictionary:", my_dict)
# Iterating through keys and values
for key, value in my_dict.items():
print(f"{key}: {value}")
```
### Sets:
#### Example 4 - Creating and Operating on Sets:
```python
# Creating a set
my_set = {1, 2, 3, 4, 4, 4}
# Adding and removing elements
my_set.add(5)
my_set.remove(2)
print("Updated set:", my_set)
```
### Sample Program for all data types
```python
# Integer
integer_var = 42
print("Integer Variable:", integer_var)
# Float
float_var = 3.14
print("Float Variable:", float_var)
# String
string_var = "Hello, World!"
print("String Variable:", string_var)
# List
list_var = [1, 2, 3, 4, 5]
print("List Variable:", list_var)
# Tuple
tuple_var = (10, 20, 30, 40, 50)
print("Tuple Variable:", tuple_var)
# Dictionary
dict_var = {'a': 1, 'b': 2, 'c': 3}
print("Dictionary Variable:", dict_var)
# Set
set_var = {1, 2, 3, 4, 5}
print("Set Variable:", set_var)
# Basic tasks
# Integer and Float operations
result = integer_var + float_var
print("Integer + Float:", result)
# String concatenation
new_string = string_var + " Have a nice day!"
print("Concatenated String:", new_string)
# List manipulation
list_var.append(6)
list_var.remove(2)
print("Modified List:", list_var)
# Dictionary operations
dict_var['d'] = 4
del dict_var['a']
print("Modified Dictionary:", dict_var)
# Set operations
set_var.add(6)
set_var.remove(3)
print("Modified Set:", set_var)
```