- Nick Congleton
IntroductionSomebody hit lists with gamma rays. Okay, so
Dictionariesaren't the Incredible Hulk, but they are supercharged in what they can do. In other languages, dictionaries are referred to as hashes, associative arrays, and associative lists. It's probably best to think of them as associative lists because that's exactly what they are. Dictionaries are lists that associate two values with one another. To think of it in terms of an actual dictionary, they associate a word, or
keywith a definition, or
value. They function sort of like a list with custom indexes.
- Nick Congleton
IntroductionThere is yet another type of loop. That loop is designed for iterating sets of data. That's right, lists. Unlike
forloops have a defined length based on the data set that they are iterating over.
forloops are used to access and modify each element in a list. To do this, they temporarily represent each element as a new variable used only within the loop.
forloops have a slightly different structure than
whileloops do. They begin with the word
for, which is followed by the temporary variable being created for the loop. Then there is the keyword
inspecifying the set of data being used, followed by the data set itself and, ultimately, a colon.
For With RangeThere is a method called
range()that either takes a single number and behaves like a list of numbers going from zero until the number before the one specified or takes two numbers separated by a comma and acts like a slice starting at the first number and listing all numbers until the number before the last one.
- Nick Congleton
IntroductionMany times in programming, you will need to repeat the same task many times. In fact, looping through and repeating an operation is one of the cornerstones of programming. After all, one of the things that computers are way better than humans at is performing repetitive tasks without getting tired or making mistakes.
One of the simplest ways to make a program repeat the same task is to use a
whileloop repeats the same block of code while a condition is true. When that condition becomes false, the loop will break, and the regular flow of code will resume.
The structure of a
whileloop is similar to what you encountered in the last guide with
if. A while loop begins with the word
whilefollowed by parenthesis containing the condition of the loop and a colon. The following lines are indented and will execute in the loop.
Infinite WhileCheck out this while loop. Try it yourself in your interpreter to see exactly what it does. You might be somewhat surprised.
# Import time for sleep import time # While loop while(True): print("looping...") time.sleep(2)What happened? Rather, what is happening? If you haven't figured out how to stop it yet, just press
whileloop will run indefinitely as long as the condition that it is given remains
True. The loop above was given
Trueas its condition, which will never not be true.
- Lubos Rendek
ObjectiveThe objective is to mount a CD/DVD block device with ISO 9660 disk image/media.
RequirementsPrivileged access to your Redhat 7 system.
- # - requires given command to be executed with root privileges either directly as a root user or by use of
- $ - given command to be executed as a regular non-privileged user
Locate Block deviceFirst we need to find a correct CD/DVD block device. To do this execute a command
# blkid /dev/xvda1: UUID="b4f59ae4-f5c8-49c2-94cf-103e10eef407" TYPE="xfs" /dev/xvda2: UUID="zcwKzx-w2EM-NH4E-wQW3-7QTu-62JZ-s6Ok0j" TYPE="LVM2_member" /dev/sr0: UUID="2016-10-19-18-32-06-00" LABEL="RHEL-7.3 Server.x86_64" TYPE="iso9660" PTTYPE="dos" /dev/mapper/rhel-root: UUID="ea637699-1d70-49ef-9a0a-54bc87d1e571" TYPE="xfs" /dev/mapper/rhel-swap: UUID="617ccf82-602c-472d-bff6-484d95530293" TYPE="swap"
- Nick Congleton
IntroductionHow can a program make a decision? Can a program choose between two or more options. Actually, it can. This isn't some kind of advanced AI concept, it's just a matter of evaluating whether or not certain conditions have been met and choosing a response.
The way that a program can evaluate a condition comes down to true and false. If something is true, do this. If it isn't true do, that. The
ifstatement is the structure for a program to pose these questions and evaluate whether or not they are true.
ifstatements can check multiple conditions and provide multiple responses. They can be used to divert code down one path or another and control the overall flow of a program. They can also be used as a gating mechanism to determine whether certain blocks of code run. Have you ever gotten a message telling you that you needed to log in to continue? That was the result of
ifhas a very simple structure. The word,
if, is followed by a set of parenthesis containing a statement that needs to be evaluated for truthfulness and a colon. The following line is indented and contains the action to be performed if the statement is true. There can be multiple actions following
ifas long as they are all indented.
if (5 ** 2 >= 25): print("It's true!") print("If is awesome!")You can resume the normal flow of the program following
ifby returning to unindented lines of code.