From 58b19d311185b1b6c11131b48e30f8c327d05b11 Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 3 Jun 2024 13:46:23 -0400 Subject: [PATCH] Add the first few basic examples --- Python/000_basics.py | 27 +++++++++++++++++++++++++++ Python/005_userinput.py | 10 ++++++++++ Python/010_formatoutput.py | 14 ++++++++++++++ Python/015_ifconditional.py | 18 ++++++++++++++++++ Python/020_whileloop.py | 14 ++++++++++++++ Python/025_tryexcept.py | 39 +++++++++++++++++++++++++++++++++++++++ Python/README.md | 3 +++ 7 files changed, 125 insertions(+) create mode 100644 Python/000_basics.py create mode 100644 Python/005_userinput.py create mode 100644 Python/010_formatoutput.py create mode 100644 Python/015_ifconditional.py create mode 100644 Python/020_whileloop.py create mode 100644 Python/025_tryexcept.py create mode 100644 Python/README.md diff --git a/Python/000_basics.py b/Python/000_basics.py new file mode 100644 index 0000000..c11c446 --- /dev/null +++ b/Python/000_basics.py @@ -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!") \ No newline at end of file diff --git a/Python/005_userinput.py b/Python/005_userinput.py new file mode 100644 index 0000000..52c1a80 --- /dev/null +++ b/Python/005_userinput.py @@ -0,0 +1,10 @@ +# Basic example of user input and printing to the console + +# Get some user input +result = input("Type anything and hit : ") +# Print a blank line +print() + +# Show the user what they typed +print("You typed:", result) +print() \ No newline at end of file diff --git a/Python/010_formatoutput.py b/Python/010_formatoutput.py new file mode 100644 index 0000000..1b5b0c6 --- /dev/null +++ b/Python/010_formatoutput.py @@ -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() \ No newline at end of file diff --git a/Python/015_ifconditional.py b/Python/015_ifconditional.py new file mode 100644 index 0000000..5fb2fbf --- /dev/null +++ b/Python/015_ifconditional.py @@ -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() \ No newline at end of file diff --git a/Python/020_whileloop.py b/Python/020_whileloop.py new file mode 100644 index 0000000..a07dcd7 --- /dev/null +++ b/Python/020_whileloop.py @@ -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() \ No newline at end of file diff --git a/Python/025_tryexcept.py b/Python/025_tryexcept.py new file mode 100644 index 0000000..166be19 --- /dev/null +++ b/Python/025_tryexcept.py @@ -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() \ No newline at end of file diff --git a/Python/README.md b/Python/README.md new file mode 100644 index 0000000..bf4495b --- /dev/null +++ b/Python/README.md @@ -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. \ No newline at end of file