Programming Basics With Examples
Programming Basics With Examples
1. Data Types
Data types define the nature of data that can be stored and manipulated in a programming
language.
Types of Data:
1. Primitive Data Types - Basic types of data that represent single values.
3. Abstract Data Types (ADTs) - Data structures designed for specific functionalities.
Example:
```python
age = 25
print(age)
```
- Floating-Point (float): Numbers with decimal points (e.g., 3.14, -0.001, 2.0)
```python
pi = 3.14159
print(pi)
```
is_raining = True
print(is_raining)
```
```python
name = "Alice"
print(name)
```
```python
```
```python
print(coordinates[1]) # Output: 20
```
```python
```
```python
numbers = {1, 2, 3, 4, 4, 5}
```
Casting:
- Integer to String:
```python
num = 100
str_num = str(num)
```
- String to Integer:
```python
str_value = "50"
int_value = int(str_value)
print(int_value) # Output: 50
```
Operators:
- Arithmetic Operators:
```python
a = 10
b=3
```
- Comparison Operators:
```python
x=5
y = 10
print(x == y) # False
```
- Logical Operators:
```python
a = True
b = False
print(a or b) # True
```
Variables:
```python
name = "John"
age = 25
print(name, age)
```
```python
PI = 3.14159
print(PI)
```
4. Strings
String Operations:
- Concatenation:
```python
first_name = "John"
last_name = "Doe"
```
- String Indexing:
```python
text = "Hello"
print(text[0]) # Output: H
print(text[-1]) # Output: o
```
- String Methods:
```python
```
Conclusion
Understanding these programming basics is crucial for working with data effectively.