Skip to content

Instantly share code, notes, and snippets.

@maf946
Last active March 9, 2021 17:34
Show Gist options
  • Save maf946/c80864b269bf2c4bfc537d566c29b5b4 to your computer and use it in GitHub Desktop.
Save maf946/c80864b269bf2c4bfc537d566c29b5b4 to your computer and use it in GitHub Desktop.
# Any line that begins with "#" is a comment, and will not be interpreted
# Printing "Hello, World"
## We have to begin with the O.G.
print("Hello, World!")
## We are using the print() "function," which is built into python. We are saying, I would like to print, and then
## including what we would like to print within the parentheses. We could also print an integer using the print function:
print(220)
## Each print statement below gets printed to its own line
print("We Are!")
print("Penn State!")
## We can also combine the text we want to print within one line, as below:
print("We Are!" + " Penn State!")
## Note that in the above I've added a space before the "P" so that it will look nice
## Sometimes it's nice to have tabular data, which looks like a table. We can do this using "\t", which is the
## equivalent of pressing the "Tab" button on your keyboard:
print("Penn State Football Career Leaders in Passing Yards")
print("Player\t\t\t\t\tYards")
print("Trace McSorley\t\t\t9,9899")
print("Christian Hackenberg\t8,427")
print("Zack Mills\t\t\t\t7,212")
# Variables
## Just like in algebra, we can use variables to store different values. For example, consider:
port_number = 81
## This sets the variable called "port_number" to the value 81. We can modify it, such as with:
port_number = port_number - 1
## Now let's see what the current value of port_number is:
print(port_number)
# Taking User Input
## So far, our programs haven't been interactive at all; they will always execute in exactly the same way
## Often, though, we want to become interactive, such as by taking input from the user. There are lots of ways to
## take input, such as by noting where a user clicks a mouse or touches a screen, or by taking input over a socket.
## For now, let's practice taking input via the keyboard:
## Add the line of code below. When you execute it, you will be prompted to enter a number; enter one, then press
## return/enter.
packet_count = input("Please enter a number of packets: ")
print("You have entered " + packet_count + " as the number of packets.")
## Now we have used a second function, input(). The print() function simply printed out whatever we told it to.
## The input() function is a little bit different, because it both displays something to the user (here, the prompt for
## the number of packets), but also "returns" something to us: the number which the user enters. Here, we are taking
## that returned number and storing it in a variable called packet_count.
# Functions
## Using built-in functions is cool, but you know what's really cool? Creating your own functions. We can do that
## very easily in Python. For example, we might want to create a function that lets you print a certain line of text
## five times in a row. Functions need to have names; for example, the print() function is called "print". Our
## function will need a name, too. Let's call it print_five. Let's begin by "defining" the function,
## which means writing the code that will be run when somebody wants to use the function. Later,
## we will actually "call" the function, i.e., use it. First, the definition:
def print_five():
print("TCP/IP")
print("TCP/IP")
print("TCP/IP")
print("TCP/IP")
print("TCP/IP")
## Note that the "def" keyword tells Python that we are defining a function. Then we name the function, then include
## empty parentheses (more on that later), and then have a colon. On the next line, and this is absolutely critical,
## we start with a "tab" press on the keyboard. In Python, tabs and lines and spacing have meaning. In this case,
## Python relies on the tabs to help it figure out which lines of code are part of the function, and which are not.
## Often, if you are seeing error messages in your script, it is because you have a problem with indentation.
## More often, it's a simple typo, or you've used the wrong variable or function name.
## OK, with that out of the way, let's actually call print_five():
print_five()
## Pretty simple, right? Whenever the above line of code appears, Python will print "TCP/IP" five times.
## But what if we want to be able to print something other than "TCP/IP"? We can do that by "passing" a value
## to a function, which we can then use within the function. So, let's make a function similar to print_five()
## which lets us do exactly that:
def print_five_x(what_to_print):
print(what_to_print)
print(what_to_print)
print(what_to_print)
print(what_to_print)
print(what_to_print)
## Let's also go ahead and call print_five_x():
print_five_x("IST")
## As you can see, we call print_five_x() and pass it the value "IST". In the definition for print_five_x(), we
## are passed a variable called what_to_print, which is pre-filled with the value from when the function was
## called, and then we can use it just like we can use any other variable.
# Looping
## To this point, our script is somewhat interactive, but will only run once, top to bottom, and then exit.
## Often we will want to have certain lines or blocks of code execute multiple times. One way to do this is to
## use the "while" loop in Python. Using the while loop, we can have a block of code execute repeatedly until a certain
## condition is met. For example, let's create a while loop that prompts a user to enter some text, and will
## keep prompting them forever until they enter the text "stop":
user_text = ""
while user_text != "stop":
user_text = input("Please enter some text; type stop to quit: ")
## Above, we first created a variable called user_text, and set it to be empty. We need to have this variable
## declared for the loop below. Everything that is below "while user_text != "stop":" and indented will run indefinitely,
## as long as the text the user enters is not equal to "stop". The "!=" operator means "is not equal to."
## Last, but not least, let's combine a few concepts together. Let's create a new funciton that will print out
## the text that a user enters, however many times the user would like.
def print_text_x(text_to_print, number_of_times):
for y in range(number_of_times):
print(text_to_print)
## We are using the for() loop here; the details aren't important for our purposes, but you can see that we have now
## "nested" indented code within other indented code. Again, having your indentation match up will be critical.
## Let's prompt the user for the text to print, and the number of times to run
text_to_print = input("What text would you like to print? ")
number_of_times = input("How many times would you like to run? ")
## Note that Python treats text and numbers differently. We want to make sure that number_of_times is treated
## as a number, so we can convert it to its integer form:
number_of_times = int(number_of_times)
## Then, call print_text_x(), passing it both variables
print_text_x(text_to_print, number_of_times)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment