Skip to content

Commit c9d3f57

Browse files
committed
21-Python 3.12 Docs
1 parent f027ab4 commit c9d3f57

5 files changed

Lines changed: 80 additions & 0 deletions

File tree

21-Python 3.12 Docs/help.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import os
2+
import sys

21-Python 3.12 Docs/new.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
songs = ['Take me back to Eden', 'Alkaline', 'Ascensionism']
2+
# print(f"This is the playlist: {", ".join(songs)}")
3+
# print(f"{list(range(1,11))}")
4+
name = "codebhaiya"
5+
print(f"{f"{f"{name}"}"}")

21-Python 3.12 Docs/op.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# print(name="Abhi") # throws an error
2+
# print(name:="Abhi")
3+
# a, *b, c = [1, 2, 3, 4, 5]
4+
# print(a)
5+
# print(b)
6+
# print(c)
7+
8+
def greet(name,/, greeting="Hello"):
9+
return f"{greeting}, {name}!"
10+
11+
print(greet(name="Abhinay"))

21-Python 3.12 Docs/operators.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
## 1. Walrus Operator (:=)
3+
Introduced in Python 3.8, the walrus operator allows assignment expressions, enabling assignment and return of a value in a single expression. This is particularly useful in loops and comprehensions.
4+
```python
5+
# Not Allowed
6+
print(is_alive=False)
7+
# Allowed
8+
print(is_active:=True)
9+
```
10+
11+
## 2. Matrix Multiplication Operator (@)
12+
Introduced in Python 3.5, the '@' operator is used for matrix multiplication, which is especially useful in numerical computing and machine learning applications.
13+
```python
14+
import numpy as np
15+
16+
a = np.array([[1, 2], [3, 4]])
17+
b = np.array([[5, 6], [7, 8]])
18+
19+
# Matrix multiplication
20+
c = a @ b
21+
print(c)
22+
```
23+
24+
## 3. New String Formatting (f-strings)
25+
Introduced in Python 3.6, f-strings provide a way to embed expressions inside string literals, using curly braces '{}'.
26+
```python
27+
name = "Abhinay"
28+
age = 21
29+
print(f"My name is {name} and I am {age} years old.")
30+
```
31+
## 4. Enhanced Unpacking
32+
Python 3.5 introduced extended unpacking with the '*' operator to allow more flexible unpacking operations.
33+
```python
34+
a, *b, c = [1, 2, 3, 4, 5]
35+
print(a) # 1
36+
print(b) # [2, 3, 4]
37+
print(c) # 5
38+
```
39+
## 5. Positional-Only Parameters
40+
Introduced in Python 3.8, positional-only parameters are defined by adding a '/' in the function signature. These parameters can only be passed positionally, not as keyword arguments.
41+
```python
42+
def greet(name, /, greeting="Hello"):
43+
return f"{greeting}, {name}!"
44+
45+
print(greet("Abhinay")) # Works
46+
# print(greet(name="Abhinay")) # Raises TypeError
47+
```
48+
49+
## 6. Keyword-Only Parameters
50+
Defined by adding '*' in the function signature, parameters following '*' can only be passed as keyword arguments.
51+
```python
52+
def greet(*, name, greeting="Hello"):
53+
return f"{greeting}, {name}!"
54+
55+
print(greet(name="Abhinay")) # Works
56+
# print(greet("Abhinay")) # Raises TypeError
57+
```
58+
These new operators and enhancements make Python more expressive and capable of handling various programming paradigms more efficiently.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
print("hello1","Hello Abhi", sep="#")
2+
print("hello2",end="\n")
3+
print("hello3",end="\n")
4+
print("hello4",end="\n")

0 commit comments

Comments
 (0)