From 6fae741d4340e255fe376ac44bf13c63591529af Mon Sep 17 00:00:00 2001 From: Maglovski Nenad <79421496+maglovskiNenad@users.noreply.github.com> Date: Mon, 11 Nov 2024 22:40:33 +0100 Subject: [PATCH] py --- _posts/2024-11-10-python.md | 115 +++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/_posts/2024-11-10-python.md b/_posts/2024-11-10-python.md index 73fa0c6..6e16875 100644 --- a/_posts/2024-11-10-python.md +++ b/_posts/2024-11-10-python.md @@ -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] ---- \ No newline at end of file +--- + +# 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: + +y = 3.14 +print(type(y)) # Output: + +z = "Python" +print(type(z)) # Output: +``` + +### 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 \ No newline at end of file