Add the first few basic examples
This commit is contained in:
parent
387d551844
commit
58b19d3111
27
Python/000_basics.py
Normal file
27
Python/000_basics.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
# This will show some very basic Python operations and actions
|
||||
|
||||
# We show output in the terminal to a user with the "print" function
|
||||
print("Hello world!")
|
||||
|
||||
# This is a variable. A variable holds information like a string (letters and such),
|
||||
# integers (whole numbers), arrays and objects (discussed later)
|
||||
# Our first "string"
|
||||
name = "Computer User"
|
||||
# We can print many things in one print command on one line by
|
||||
# separating them with commas
|
||||
print("Your name is:", name)
|
||||
|
||||
# Our first number
|
||||
# The "=" here is an "assignment operator"
|
||||
# meaning "assign the value on the right to the variable on the left"
|
||||
# (i.e. assign age equal to 32)
|
||||
age = 32
|
||||
print("Your age is:", age)
|
||||
|
||||
# Basic math:
|
||||
# Python includes math operators for addition, subtraction,
|
||||
# multiplication and division (and many others).
|
||||
age = age + 1
|
||||
# Age is now equal to 33
|
||||
# Someone had a birthday!
|
||||
print("Happy Birthday! You are now", age, "years old!")
|
10
Python/005_userinput.py
Normal file
10
Python/005_userinput.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
# Basic example of user input and printing to the console
|
||||
|
||||
# Get some user input
|
||||
result = input("Type anything and hit <Enter>: ")
|
||||
# Print a blank line
|
||||
print()
|
||||
|
||||
# Show the user what they typed
|
||||
print("You typed:", result)
|
||||
print()
|
14
Python/010_formatoutput.py
Normal file
14
Python/010_formatoutput.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Basic example of formatting output printed to the console
|
||||
|
||||
# Get the user's age
|
||||
age = input("How old are you?: ")
|
||||
# Print a blank line
|
||||
print()
|
||||
|
||||
# Show the user what they typed
|
||||
# the "f" before the quote means that this string is "formatted"
|
||||
# Formatted strings can use curly bracket notation to embed
|
||||
# variables inside a string. This is handy as it handles variable
|
||||
# type conversion automatically (i.e. integer to string)
|
||||
print(f"You are {age} years old!")
|
||||
print()
|
18
Python/015_ifconditional.py
Normal file
18
Python/015_ifconditional.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Basic example of formatting output printed to the console
|
||||
|
||||
# Get the user's name
|
||||
name = input("What is your full name?: ")
|
||||
# Print a blank line
|
||||
print()
|
||||
|
||||
# Show the user what they typed
|
||||
print(f"Your name is: {name}")
|
||||
# Here we will do the "if" conditional. A conditional means
|
||||
# "compare these things" and "if" means "does this whole thing evaluate to True".
|
||||
# In this case we are using the string function/method (see below)
|
||||
# to try and find a space character in the name and print a message if not found.
|
||||
# "-1" here means "not found"
|
||||
# Find method: https://www.w3schools.com/python/ref_string_find.asp
|
||||
if name.find(" ") == -1:
|
||||
print("You seem to have only entered a single name. Are you a rock star?")
|
||||
print()
|
14
Python/020_whileloop.py
Normal file
14
Python/020_whileloop.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Basic example of a while loop
|
||||
|
||||
# While loops "do something" as long as the condition
|
||||
# defined at the start of the loop is True
|
||||
|
||||
print("Here are the numbers from 1 to 10...")
|
||||
print()
|
||||
|
||||
number = 1
|
||||
while number <= 10:
|
||||
print(f"Number: {number}")
|
||||
number = number + 1
|
||||
|
||||
print()
|
39
Python/025_tryexcept.py
Normal file
39
Python/025_tryexcept.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
# Basic example of using try/except to catch errors
|
||||
|
||||
# Get the user's age. The input function always returns
|
||||
# a string. In order to do math operations on user input
|
||||
# we have to convert it to an number/integer. However, any user
|
||||
# input that is NOT a number (integer in this case) will cause
|
||||
# a fatal error and the program will crash. We use a try/except
|
||||
# to catch this event and keep requesting input until the value
|
||||
# is a valid integer.
|
||||
# We loop forever asking for a valid age. The initial unacceptable
|
||||
# value for age means the loop will be executed at least once.
|
||||
age = -1
|
||||
while age < 0:
|
||||
try:
|
||||
# Get the age
|
||||
age = int(input("How old are you?: "))
|
||||
# If the age is less than zero, display an error
|
||||
if age < 0:
|
||||
print()
|
||||
print("Error! Valid ages are greater than or equal to zero!")
|
||||
print()
|
||||
except:
|
||||
# If the integer conversion fails (i.e. a non-number was entered) show error
|
||||
print()
|
||||
print("Error! You must only enter a whole number!")
|
||||
print()
|
||||
|
||||
# Print a blank line
|
||||
print()
|
||||
|
||||
# Calculate
|
||||
|
||||
# Show the user what they typed
|
||||
# the "f" before the quote means that this string is "formatted"
|
||||
# Formatted strings can use curly bracket notation to embed
|
||||
# variables inside a string. This is handy as it handles variable
|
||||
# type conversion automatically (i.e. integer to string)
|
||||
print(f"You are {age} years old!")
|
||||
print()
|
3
Python/README.md
Normal file
3
Python/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Python Examples
|
||||
|
||||
This folder will contain examples of basic concepts for both syntax and logic. Complex examples may be in sub-folders, while simple examples are usually in the top level folder.
|
Loading…
Reference in New Issue
Block a user