Skip to content

Commit

Permalink
py
Browse files Browse the repository at this point in the history
  • Loading branch information
maglovskiNenad committed Nov 11, 2024
1 parent 409b1f6 commit 6fae741
Showing 1 changed file with 114 additions and 1 deletion.
115 changes: 114 additions & 1 deletion _posts/2024-11-10-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,117 @@ title: Python
date: 2024-11-10 21:10:44 +08000
categories: blog
tags: [Python,Variables,Data,Data Types,Control Flow,Python List,Loops,Dictionaries,Scope,CSV,List Complrehension]
---
---

# Python:

Python is a high-level, interpreted programming language known for its readability, flexibility, and vast ecosystem of libraries. Designed with simplicity in mind, Python’s syntax is straightforward, making it an excellent choice for beginners and professionals alike. Created by Guido van Rossum in the late 1980s, Python has evolved into one of the most popular programming languages in the world.

## Variables in Python

In Python, declaring a variable is as simple as assigning a value to a name using the = operator:

```Python
# Examples of variables
name = "Alice" # string variable
age = 25 # integer variable
height = 5.7 # float variable
is_student = True # boolean variable

```

### Variable Naming Rules

- Variable names must start with a letter (a-z, A-Z) or an underscore _.
- Variable names cannot start with a number.
- Only letters, numbers, and underscores are allowed in variable names.
- Variable names are case-sensitive (age and Age would be two different variables).

1. Examples of valid variable names:
```Python
first_name = "Alice"
_age = 30
height_in_cm = 175

```

2. Examples of invalid variable names:
```Python
2nd_variable = "invalid" # Starts with a number
first-name = "invalid" # Contains a hyphen
```

### Dynamic Typing

- Integers (int): Whole numbers (e.g., 10, -5)
- Floating Point Numbers (float): Numbers with decimals (e.g., 5.7, -3.14)
- Strings (str): Text data (e.g., "hello", "Python")
- Booleans (bool): Logical values True or False
- Lists (list): Ordered, mutable collection of items (e.g., [1, 2, 3])
- Tuples (tuple): Ordered, immutable collection of items (e.g., (1, 2, 3))
- Dictionaries (dict): Key-value pairs (e.g., {"name": "Alice", "age": 25})

```Python
x = 42
print(type(x)) # Output: <class 'int'>

y = 3.14
print(type(y)) # Output: <class 'float'>

z = "Python"
print(type(z)) # Output: <class 'str'>
```

### Reassigning Variables

```Python
number = 10
print(number) # Output: 10

number = 15 # Reassign with a new integer value
print(number) # Output: 15

number = "ten" # Reassign with a string value
print(number) # Output: ten

```

### Constants

Although Python does not have a built-in way to declare constants (variables that should not change), it’s a common convention to use all-uppercase names for variables intended to be constant:

```Python
PI = 3.14159
GRAVITY = 9.8
```

### Multiple Variable Assignment

```Python
a, b, c = 1, 2, 3
print(a) # Output: 1
print(b) # Output: 2
print(c) # Output: 3
x = y = z = 100
print(x, y, z) # Output: 100 100 100
```

### Variable Scope

- Global Variables: Defined outside of a function and can be accessed anywhere in the code.
- Local Variables: Defined inside a function and can only be accessed within that function.

```Python
global_var = "I am global"

def my_function():
local_var = "I am local"
print(global_var) # This works
print(local_var) # This also works

my_function()
print(global_var) # This works
# print(local_var) # This would cause an error, as local_var is not accessible outside the function
```

## Data

0 comments on commit 6fae741

Please sign in to comment.