78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
# 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!")
|
|
|
|
# By default, print() will put a new line at the end of ever print.
|
|
# If you want to stay on the same line (so you can add to it later)
|
|
# you must change the end character to be blank
|
|
print("This is on a line.", end='')
|
|
print(" This is on the same line!")
|
|
|
|
# In Python (and other languages) we can store and manipulate data
|
|
# by using variables. Variables are comprised of a name (the label
|
|
# we use to refer to that variable), and a value.
|
|
# We use an "assignment" to equate a name and a value.
|
|
# A variable can have nearly any value. Typical simple values may be
|
|
# a string (letters and such), integers (whole numbers),
|
|
# floats (numbers with both whole and fractional parts),
|
|
# and arrays and objects (discussed later).
|
|
|
|
# Our first "string"
|
|
# Below is a variable named "name".
|
|
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)
|
|
|
|
# There are several basic data types for variables:
|
|
# int: Integer number (no decimal)
|
|
# float: Floating point number (has a decimal component)
|
|
# str: String. This is a collection of characters bounded by single or double quotes
|
|
# bool: Boolean. This is either True or False
|
|
# bytes: A string that holds binary data
|
|
# Created thusly: b"\x28\x67\x67\x29" (converts to the ascii characters "(gg)")
|
|
# More info: https://realpython.com/python-data-types/
|
|
|
|
# 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!")
|
|
|
|
# Simple math operators can be combined for the sake of brevity.
|
|
# These are called "compound" operators
|
|
# age = age + 1
|
|
# is the same as
|
|
# age += 1
|
|
#
|
|
# Basic compound operators: += -= *= /= (/= is a floating point operator)
|
|
# (A special //= which returns integer)
|
|
unused_age = 100
|
|
print(f"Unused Age: {unused_age}")
|
|
unused_age -= 1 # unused_age is now 99
|
|
print(f"Unused Age: {unused_age}")
|
|
unused_age += 3 # unused_age is now 102
|
|
print(f"Unused Age: {unused_age}")
|
|
unused_age *= 4 # unused_age is now 408
|
|
print(f"Unused Age: {unused_age}")
|
|
unused_age //= 2 # unused_age is now 204
|
|
print(f"Unused Age: {unused_age}")
|
|
|
|
# There is a common operator used in programming called the "modulo" or "modulus".
|
|
# This operator is a division operator that returns the remainder after
|
|
# the division is complete.
|
|
unused_age %= 100
|
|
print(f"Unused Age: {unused_age}")
|
|
|
|
|