Python Basics: Unterschied zwischen den Versionen
Aus MattWiki
Matt (Diskussion | Beiträge) Die Seite wurde neu angelegt: „This article contains my notes on the Python programming language as of Python 3.x. == Strings == === F-Strings === <syntaxhighlight lang="python3"> a = "Munich" b = "Frankfurt" c = "Berlin" # f-String print(f"We are doing a road trip from {a} through {b} to {c}") </syntaxhighlight> === Template Strings === <syntaxhighlight lang="python3"> a = "Munich" b = "Frankfurt" c = "Berlin" # Template string template = "We are doing a road trip from {} throug…“ |
Matt (Diskussion | Beiträge) |
||
(14 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
This article contains my notes on the Python programming language as of Python 3.x. | This article contains my notes on the Python programming language as of Python 3.x. | ||
== Operators == | |||
See [[Python Operators]] | |||
== Built-in Functions == | |||
https://docs.python.org/3/library/functions.html | |||
== Strings == | == Strings == | ||
=== | === Convert a List of Strings to a String === | ||
<syntaxhighlight lang="python3"> | |||
list1 = ['Hello', 'World'] | |||
print(' '.join(list1)) | |||
# Output: | |||
# Hello World | |||
</syntaxhighlight>When used with a list of elements, which are not of type string, the elements need to be converted to string first. | |||
=== Template Strings === | |||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
a = "Munich" | a = "Munich" | ||
Zeile 9: | Zeile 22: | ||
c = "Berlin" | c = "Berlin" | ||
# | # Template string | ||
print( | template = "We are doing a road trip from {} through {} to {}" | ||
print(template.format(a, b, c)) | |||
# equivalent to: | |||
print("We are doing a road trip from {} through {} to {}".format(a, b, c)) | |||
# equivalent to: | |||
print("We are doing a road trip from {place1} through {place2} to {place3}".format(place3=c, place2=b, place1=a)) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === F-Strings === | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
a = "Munich" | a = "Munich" | ||
Zeile 20: | Zeile 41: | ||
c = "Berlin" | c = "Berlin" | ||
# | # f-String | ||
print(f"We are doing a road trip from {a} through {b} to {c}") | |||
print( | |||
# | </syntaxhighlight>Further reading: | ||
print(" | |||
</syntaxhighlight> | * https://realpython.com/python-string-formatting/ | ||
* https://docs.python.org/3/whatsnew/3.12.html#pep-701-syntactic-formalization-of-f-strings | |||
* https://docs.python.org/3/library/string.html#template-strings | |||
=== Format Specification === | |||
Format Number to string with one decimal place and a % as suffix. Not needed decimal places rounded as follows:<syntaxhighlight lang="python">print("{:.1f}%".format(2.25)) #output: 2.2% | |||
print("{:.1f}%".format(2.26)) #output: 2.3%</syntaxhighlight> | |||
See https://docs.python.org/3/library/string.html#formatspec | |||
== Classes and Object Methods == | |||
https://docs.python.org/3/reference/datamodel.html#emulating-container-types | |||
== Command Line == | |||
https://docs.python.org/3/using/cmdline.html | |||
=== Executing functions from command line === | |||
A function called <code>myfunction</code> from <code>myscript.py</code> can be executed directly from the command line like this: | |||
python -c 'import myscript; myscript.myfunction(var1, var2) | |||
== Modules == | |||
See [[Python Built-in Modules]] | |||
[[Kategorie:Python]] | [[Kategorie:Python]] |
Aktuelle Version vom 14. Februar 2025, 09:21 Uhr
This article contains my notes on the Python programming language as of Python 3.x.
Operators
See Python Operators
Built-in Functions
https://docs.python.org/3/library/functions.html
Strings
Convert a List of Strings to a String
list1 = ['Hello', 'World']
print(' '.join(list1))
# Output:
# Hello World
When used with a list of elements, which are not of type string, the elements need to be converted to string first.
Template Strings
a = "Munich"
b = "Frankfurt"
c = "Berlin"
# Template string
template = "We are doing a road trip from {} through {} to {}"
print(template.format(a, b, c))
# equivalent to:
print("We are doing a road trip from {} through {} to {}".format(a, b, c))
# equivalent to:
print("We are doing a road trip from {place1} through {place2} to {place3}".format(place3=c, place2=b, place1=a))
F-Strings
a = "Munich"
b = "Frankfurt"
c = "Berlin"
# f-String
print(f"We are doing a road trip from {a} through {b} to {c}")
Further reading:
- https://realpython.com/python-string-formatting/
- https://docs.python.org/3/whatsnew/3.12.html#pep-701-syntactic-formalization-of-f-strings
- https://docs.python.org/3/library/string.html#template-strings
Format Specification
Format Number to string with one decimal place and a % as suffix. Not needed decimal places rounded as follows:
print("{:.1f}%".format(2.25)) #output: 2.2%
print("{:.1f}%".format(2.26)) #output: 2.3%
See https://docs.python.org/3/library/string.html#formatspec
Classes and Object Methods
https://docs.python.org/3/reference/datamodel.html#emulating-container-types
Command Line
https://docs.python.org/3/using/cmdline.html
Executing functions from command line
A function called myfunction
from myscript.py
can be executed directly from the command line like this:
python -c 'import myscript; myscript.myfunction(var1, var2)