Python Basics: Unterschied zwischen den Versionen

Aus MattWiki
Keine Bearbeitungszusammenfassung
 
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt)
Zeile 8: Zeile 8:
== Strings ==
== Strings ==


=== F-Strings ===
=== Template Strings ===
<syntaxhighlight lang="python3">
<syntaxhighlight lang="python3">
a = "Munich"
a = "Munich"
Zeile 14: Zeile 14:
c = "Berlin"
c = "Berlin"


# f-String
# Template string
print(f"We are doing a road trip from {a} through {b} to {c}")
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>


=== Template Strings ===
=== F-Strings ===
<syntaxhighlight lang="python3">
<syntaxhighlight lang="python3">
a = "Munich"
a = "Munich"
Zeile 25: Zeile 33:
c = "Berlin"
c = "Berlin"


# Template string
# f-String
template = "We are doing a road trip from {} through {} to {}"
print(f"We are doing a road trip from {a} through {b} to {c}")
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 {2} through {1} to {0}".format(c, b, a))
</syntaxhighlight>Further reading:  
</syntaxhighlight>Further reading:  



Aktuelle Version vom 6. Januar 2025, 14:53 Uhr

This article contains my notes on the Python programming language as of Python 3.x.

Operators

https://www.codecademy.com/resources/docs/python/operators

https://docs.python.org/3/reference/lexical_analysis.html#operators

Strings

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:

Built-in Functions

https://docs.python.org/3/library/functions.html


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)