Skip to content

Commit 86d7405

Browse files
authored
Merge branch 'master' into python-download-file-from-url
2 parents 030206a + cd9be54 commit 86d7405

29 files changed

Lines changed: 473 additions & 2 deletions

ipython-console/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code Snippets and IPython Cheat Sheet
2+
3+
This folder contains code resources and an IPython magic command cheat sheet based on the content of the Real Python tutorial [Unlock IPython's Magical Toolbox for Your Coding Journey](https://realpython.com/ipython-interactive-python-shell/).

ipython-console/employee.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
first_name = "Han"
2+
last_name = "Solo"
3+
department = "Accounts"
4+
salary = 1000
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import IPython
2+
3+
first_name = "Han"
4+
last_name = "Solo"
5+
department = "Accounts"
6+
print(f"{first_name} {last_name}")
7+
8+
IPython.embed()
9+
10+
salary = 1000

ipython-console/increment.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def salary_increment(salary):
2+
"""Calculate the new salary after applying an increment.
3+
4+
Args:
5+
salary (int): The current salary.
6+
7+
Returns:
8+
str: A string indicating the new salary after increment.
9+
"""
10+
increment = salary / 10
11+
new_salary = increment + salary
12+
return f"Your New Salary is: {new_salary}"
136 KB
Binary file not shown.

python-312/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ You can learn more about Python 3.12's new features in the following Real Python
1414

1515
- [Python 3.12 Preview: Ever Better Error Messages](https://realpython.com/python312-error-messages/)
1616
- [Python 3.12 Preview: Support For the Linux `perf` Profiler](https://realpython.com/python312-perf-profiler/)
17+
- [Python 3.12 Preview: More Intuitive and Consistent F-Strings](https://realpython.com/python312-f-strings/)
1718

1819
You'll find examples from all these tutorials in this repository.
1920

@@ -117,6 +118,24 @@ $ cat traces_censored.txt | flamegraph.pl --minwidth 10 > flamegraph.svg
117118

118119
See [Support For the Linux `perf` Profiler in Python 3.12](https://realpython.com/python312-perf-profiler/) for more information.
119120

121+
### New F-String Implementation and Syntax Formalization
122+
123+
Once you have 3.12 installed, open any file from the `f-string/` folder. Uncomment the code as needed. [Run the scripts](https://realpython.com/run-python-scripts/) from your command line or execute the code directly in a [REPL](https://realpython.com/python-repl/) session. You'll see the new f-string implementation in action.
124+
125+
For example, go ahead and run the following command:
126+
127+
```console
128+
$ python backslashes.py
129+
Hello
130+
World!
131+
I
132+
am
133+
a
134+
Pythonista!
135+
```
136+
137+
In this example, you can see how the new implementation of f-strings allows you to include backslashes in embedded expressions. This wasn't possible with f-strings in earlier versions of Python.
138+
120139
## Authors
121140

122141
- **Martin Breuss**, E-mail: [martin@realpython.com](martin@realpython.com)

python-312/f-string/backslashes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
words = ["Hello", "World!", "I", "am", "a", "Pythonista!"]
2+
3+
# Uncomment the following line and run it in Python 3.12
4+
# print(f"{'\n'.join(words)}")

python-312/f-string/comments.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
employee = {
2+
"name": "John Doe",
3+
"age": 35,
4+
"job": "Python Developer",
5+
}
6+
7+
# Uncomment the following code and run it in Python 3.12
8+
# f"""Storing employee's data: {
9+
# employee['name'].upper() # Always uppercase name before storing
10+
# }"""

python-312/f-string/nested.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Uncomment the following code and run it in Python 3.12
2+
3+
# f"{
4+
# f"{
5+
# f"{
6+
# f"{
7+
# f"{
8+
# f"Deeply nested f-string!"
9+
# }"
10+
# }"
11+
# }"
12+
# }"
13+
# }"

python-312/f-string/quotes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
employee = {
2+
"name": "John Doe",
3+
"age": 35,
4+
"job": "Python Developer",
5+
}
6+
7+
# Uncomment the following line and run it in Python 3.12
8+
# f"Employee: {employee["name"]}"

0 commit comments

Comments
 (0)