Editing <span id="firstHeadingTitle">Python Examples</span>
Read
Edit
View history
Not logged in
Talk
Contributions
Create account
Log in
Editing
Python Examples
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Calculate the average of three numbers == avg = (num1+num2+num3)/3 == Display a variable inside an f string and round it to only have 2 decimals, like with money. (the end result looks like this: <code>1000.00</code>) == monthly_payment = 1000.0000 print(f'The monthly payment is {monthly_payment:.2f}.') == Display a large number and add commas in-between every 3 digits. (the end result looks like this: <code>1,000,000,000</code>) == large_number = 1000000000 print(f'{large_number:,}') == Display a value using a minimum field width. This is useful for spacing in a cli or gui. (the end result looks like this: <code>The number is ________99</code>) == number = 99 print(f"The number is {number:10}") == Align the text when a field width is specified. The <code><</code> can be substituted for a <code>></code> or <code>^</code> to align right or center respectively. == number = 40 print(f"{number:<10}") == Usage of the walrus operator to shorten code == # Without the walrus operator line = input("Enter a line (empty to quit): ") while line != "": print(f"You entered: {line}") line = input("Enter a line (empty to quit): ") # With the walrus operator while (line := input("Enter a line (empty to quit): ")) != "": print(f"You entered: {line}") == Exiting a while loop with a <code>break</code> statement == negativenumbers=0 while True: number = input() if number == "stop": print(negativenumbers) break else: if int(number) < 0: negativenumbers+=1 == Use case for <code>pass</code> and <code>break</code> statement == while True: table = int(input("Enter a number from 1 through 100: ")) if (table > 100) or (table < 1): pass else: for number in range(1,11): print(f"{table} x {number} = {table*number}") break == Use case for len() function inside of range() function to run code for every value in a list == for i in range(len(items)): if items[i] < 0: items[i]=0 == Switch Statement in python (called a match case statement) == lang = input("What's the programming language you want to learn? ") match lang: case "JavaScript": print("You can become a web developer.") case "Python": print("You can become a Data Scientist") case "PHP": print("You can become a backend developer") case "Solidity": print("You can become a Blockchain developer") case "Java": print("You can become a mobile app developer") case _: print("The language doesn't matter, what matters is solving problems.") == List comprehension statement. Basically lets you do what you would with a for loop, but in less lines of code. == plus_one = [i + 1 for i in my_list]
Summary:
Please note that all contributions to Lucca's Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Lucca's Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
🎨 Theme Settings
Quick Presets
Code & Pre Block Text Color
Code / Pre Text Color
Background
Primary Background
Secondary Background
Tertiary Background
Hover Background
Text
Primary Text
Secondary Text
Muted Text
Accent & Borders
Accent
Accent Dim
Border
Border 2
Header & Sidebar
Header Start
Header End
Sidebar Background
Status Colors
Success
Danger
Warning
Reset to Default
Save & Close