Cybrary IntroToPython StudyGuide 1
Cybrary IntroToPython StudyGuide 1
Intro to Python
Created By: Ioana Avram, Teaching Assistant
Module 1: Just the basics
Lesson 1.01: Course introduction
Skills Learned From This Lesson: It’s an introduction to the course, no skills here
● Instructor is Joe Perry
● You need a computer for this course
● VM knowledge also helpful
● Know the Linux command line too, will probably use that 90% of the time, but not
mandatory
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
1
● There are 3 boolean operators, AND, OR and NOT and 2 values which are true and
false
● Truth tables are tables of values (true and false) and statements (using operators like
and, or and not)
● A table has combinations of 2 to the power of the inputs ,so 2 inputs is
2^inputs=combinations
● AND = logical conjunction
○ Both statements/values have to be true for the whole thing to be true
● OR = logical disjunction
○ Only one statement/value needs to be true for the whole thing to be true
● NOT = logical inversion
○ Basically inverts the statement/value, if it's true it will change to false, if its false it
will change to true
●
○
● Law of Association
○ Means order doesn’t matter in statements
○ (A.B).C is the same as A.(B.C)
○ (A+B)+C is the same as A+(B+C)
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
2
○ (truth table for law of association)
● Law of Commutation
○ Order also doesn’t matter here if you’re doing a single operation
○ A.B is the same as B.A
○ A+B is the same as B+A
○
● Law of Distribution
○ A.(B+C) is basically (A.B) + (A.C)
○ A+(B.C) is (A+B).(A+C)
○ Here’s the truth table from the video
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
3
○ (truth table for law of distribution)
● DeMorgan’s Law
○ Inverse of A.B is the same as the inverse of A + inverse of B
○
● New Operation: XOR
● XOR is exclusive OR or logical exclusion
● Usually OR means 1 or both values have to be true for the whole statement to be true
● But with XOR it has to be only one v alue. If both values are true in an XOR statement
then the statement is false. If both are false, the statement is false.If only one value is
true, the statement is true.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
4
Lesson 1.3: Variables
Skills Learned From This Lesson: Variable concepts, numbers, strings, other structures
● Variables are just things you place other things in.
● You have data and you get data from programs, maybe other users etc
● How do you hold on to the data so you can pass it on?Or analyse it, or process it or
anything like that?
● With variables!
● Therefore variables store data.
● It’s like math where you have to solve for X..We don’t know what X is, but X holds the
data that we need to solve for. Therefore X is a variable.
● Number variables
○ Basically just numbers. Variables can hold all types of numbers
○ There’s different types of number variables. Int, float, double (which is ...float)
and so on.
● Strings
○ Strings are character ‘A’ or can be symbols or can be whole sentences or
paragraphs. They can be numbers too, but won't be treated as numbers.
○ Mostly printable characters.
○ All strings are enclosed with quotes. Double quotes or single quotes. Just don’t
use both at the same time, it confuses it.
● Structures
○ Structures are basically anything really. Abstract types
○ They can be arrays, objects, dictionaries, classes etc etc
○ Most of the times they’re user defined. Except stuff like arrays and dictionaries.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
5
○
● Elif
○ Elif or else if is the next part of the if statement
○ Basically if the first statement is false an elif statement will run.
○ You can have as many elifs as you want. Don’t have too many or the code won’t
be as readable.
● Else
○ Else goes at the end of an if statement
○ It is the default option
○ So basically if this do this, else if this do this
○ After all options are done and all are false, else is the last option
○ Else do this
●
●
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
6
○ Set an end condition, for example the variable has to be small, larger or equal to
a final value.
○ Then you increment or decrement that variable until it hits the specified condition
or final value.
○ And while that’s happening you can make it do any number of actions. For
example print something a number of times, print all the options in a list, all the
items in an array etc
●
●
● Another sample while loop
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
7
Lesson 1.7: Logic to Pseudocode part 1
Skills Learned From This Lesson: Python syntax and the command line, python scripts and help
● If you type python in the command line terminal it will give you an interpreter shell
● You can type commands here (but not full scripts)
● But as Joe says if you type python, depending on what python you have installed, it
might give you python 2 instead of 3. So, be careful with that. To get specifically to the
python 3 shell type python3.
● You could probably write a small script in the python interpreter, with if statements and
all that. But it will just be there, in the interpreter, it won't save or anything.And it probably
won’t run more than once.
● A good thing about the python interpreter is that you can test out any command you
want, or write code snippets if you need to see if your code is written correctly for
example.
● Python whitespace:
○ Python is delineated by whitespace indentation.
○ As in it uses whitespace to know which code belongs to which code.
○ You can use both tabs and the space bar to add whitespace. But you must be
consistent. Using both tabs and spaces will confuse Python and break whatever
script or program you’re trying to write. And that is because Python sees tabs and
spaces as different.
● An example of Python code from Joe’s video
●
● The Python 3 interpreter
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
8
●
●
● This is usually at the start of a python script. It’s not mandatory but its usually there to
allow for a python script to be executable.
● Joe also talks about the vim editor.
● Writing a python script is not like writing code in the interpreter. Because after all it’s just
a doc, a file. The code doesn’t run until you execute the .py file.
● Here’s the script in vim
●
● To run the new python script, depends on whether you included the shebang line.
● With the line, you can make the file executable with chmod, chmod 755 or 777
respectively and then run it like so, ./pythonscript.py.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
9
● Without the line you will have to write python3 (or just python, if you want 2 instead) and
then the name of the script.
●
● This command opens up the help module for python.
●
● Like so.
● This is the help page for the print function
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
10
●
● The help function is basically like typing man in linux, if you wanna find out about a
command or program. It is just the Python manual.
● Now, onto the dir function. Again same as the dir/ls commands. But in Python dir deals
with namespaces.
● A namespace is the current environment where you’re executing code.
● Dir without any commands
●
● If you make a variable and assign it something, it will show up in the dir function.
● If you look up that variable you just made, it will pull up all the functions for that particular
type. For example, if it’s a number, it will show all the number functions, if it’s a string, all
the string functions. And so on, and so on.
● And lastly, the python documentation itself
●
●
Lesson 1.10: Python basics part 1
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
11
Skills Learned From This Lesson: Python data types, strings and numbers
● “Variables are tupperware for you brain” --Joe Perry
● This lesson will be on 4 python data types.
● Strings
○ An example of a string variable
○
○ You can print the string with just the variable for example, print (hw);
○ Or you can print the string directly like print (“Hello world”);
○ Joe recommends having the string in a variable and printing the variable. In
Python its relatively okay to print the string directly.
○ But generally one should have it in a variable for better security in languages like
SQL and the like. It’s called parameterized input, but that’s for another course.
This is an intro to Python course.
● Numbers:
○ There’s different types of number variables in programming
■ Int
■ Float/Double
■ Boolean (bool)
■ Long int
■ Short int
■ Byte
■ Chars
■ etc..etc
○ But in Python we don’t have these types exactly. We just have plain numbers.
○ Number variables work the same way as string variables. But string variables
have quotes around them and numbers don’t. If a number has quotes around it, it
is classified as a string not a number.
○ An example of number variables
○
○ With division, Python assigns a float automatically to the result.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
12
○ Like so
○
○ You can do maths in Python
○ One example of Python math operations
○
○ You can do all the math operations in Python. And store the results in a variable.
○ Like so
○
○ If you print that variable, you will get the result of that operation
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
13
○
○ To see the contents of the list variable, either type the variable name or print it
○ Like so
○ In a script, to output the contents of a list variable, you need to use the print
command, but in the interpreter you don't have to.
○ You can do a bunch of operations on lists. You can use them with for loops,
make other lists and combine them into one, make lists inside lists inside
lists(listception!) and so on.
○ Because you can combine lists with other lists, they can contain multiple types of
data. You don't have to have just 1 data type, like in traditional array variables.
● Dictionaries
○ Example of dictionary in python
○
○ In lists, you access specific items by using their index values
○ In dictionaries you access specific values by using their keys.
○ Like so
○ If you enter a key not in the dictionary (or list) you’ll get an error
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
14
○
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
15
Lesson 2.2: If/Elif/else
Skills Learned From This Lesson: Apply logic in Python code, use and structure of If statement
in Python code
● Starting from your choice terminal we open python3 to get our interpreter
● With if statements as you know, we’re making a decision. For example:
● If this action is true, then do this action. If not move on to the next action. If no action in
the list is true use the default action. Maybe print a message or something.
● You have an initial value in a variable. How you got the value doesn’t really matter. It
could be coded in, taken as input, taken from another program or part of code.
● We can use that initial value or values to run it through an if statement. That will run a
series of decisions and return a result. What decisions and how many is really up to
you.
● Here is an example of an if statement in Python
●
● We have the variables x y and z with values in them.
● Now we can use any one of them (or even multiple at the same time), perform a check
and then an action based on the result of that check. If it returns true do an action, if its
false do a different action.
● Here we’re saying if the variable y is greater than 2, print y-2. That prints 8.
● But what if the variable y is less than 2 (or any other variable defined)?
● We used the elif keyword. Elif is the Python else if. We will also use the variable z for
comparison.
● So all we have to do is change the 2 for the variable z.
● Then write elif y is less than z (after you changed the y > 2 statement).
● If y is less than z we will just print the z variable, like so
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
16
● (If statement with an elif condition)
● And at the end we print an else to end the if statement. The else condition like it was
said earlier is just the default or last check. If all the checks for a variable or number of
variables return false the if statement will default to the else condition.
● Like so
●
● For extra practice there is an if statement document in the supplementals
● One last example of if statements in a script document
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
17
● This is one example of what a for loop looks like in Python
●
● Small note: the range option is non inclusive. It won’t print to 10 if you specify 10, it will
print to 9.
● With range Python assumes you want 10 numbers, not numbers 0-10. Hence why it only
prints to 9 and not 10. If you say to 11, it will print to 10 but not 11. And so on.
● The output of that example
●
● Example 2 of for loops
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
18
● The variable is an array containing string characters. What the for loop will do is iterate
through that array and print out the items. This type of for loop is actually quite common
for iterating menu options and that sort of thing. As output you will get the actual items in
the array, the strings.
● Like so.
● Now we move onto using the else statement in a for loop. For that we need to know the
concept of a break statement.
● A break statement is used in loops pretty often.
● What it does is if a certain condition is met or not met as it were, the break statement will
execute and the loop will end, not regarding what stage the loop is at. Break statements
as the name says, break the loop.
● Example 3 of for loops
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
19
● Now we introduce break as explained
●
● Break is useful because if you have long lists and arrays for thousands or millions of
items and you just want a few specific ones, using break will immediately end the loops
once you found what you were looking for instead of going through the whole or array,
potentially saving a lot of time and money.
● After learning about the break statements we can add in an else to our for loop.
● Here’s the example
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
20
Lesson 2.4: While in Python (While Loops)
Skills Learned From This Lesson: While loops in Python
● While loops are another type of loops you can have
● Here’s an example
●
● It is important to note that you must have the increment at the end of the loop, after the
print statement.
● If you don't have it, the loop is never going to end. Because a while loop works like, while
this variable is in this condition keep doing the action. So for example while the variable
is less than 10, keep printing numbers and incrementing.
● When the variable reaches 10, stop printing numbers. But as long as the variable is
under 10 the loop will continue to print numbers forever.
● Example 2 of a while loop:
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
21
● (Example 2 of a while loop)
●
● If you just want numbers that dont give a remainder, add the break statement.
● That will give you 3. 3 is cleanly divisible by 3 and leaves no remainder.
● If you don't add the break statement you’ll just get what is divisible by 3. (3, 6 and 9)
● The continue keyword can also be used to do the same thing as not having the break
keyword in.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
22
● Continue example:
●
● Continue is similar to break, it skips the loop if you cant find the variable or value the
loop is looking for.It doesn’t end the loop like break does. It just skips it over and moves
on to the rest of the code, so that the application keeps running and doesn't stop
because break said to.
● Pass:
●
● Pass just skips the if statement. So there is an if statement in place, but if you add pass
the code before it does not execute. Useful for stand in code, if you don't have it
functioning or done yet and don't want to execute that code just yet.
● So, we get this.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
23
● Instead of the regular 3,6,9 we would usually get.
●
● This part below is where you call your functions with your arguments
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
24
Lesson 2.7: Strings in Python
Skills Learned From This Lesson: string methods, string slicing, user input
● String in Python in more detail.
● We can use dir to look at string functions and variables, like we discussed earlier on.
● These will be specifically string functions, functions and methods that deal with strings.
● Methods are functions that are attached to an object
● Example of the upper function which capitalises a string
●
● The lower function is the opposite. It makes a string all lower case.
●
● Here’s an example
● You can use it if you have a list of names or items and you’re looking for a specific one
● String slicing:
● An example:
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
25
● W is the 8th character.
● You can also print out not only single characters/letters but a specific set or chunk.
● For example print (x[0:4]) will print the first 4 characters but not the 5th (the one at
position 4).
● But you don't have to start at 0. You can start at any index you want.
● For example print (x[3:4]) prints out the letter L, in Hello.
● This wont work however
●
● To print backwards you have to add an extra step, which will allow it to print backwards.
● Like so
●
● This prints out the whole string backwards
●
● You can use different amounts to step and give different letters
● For example this gives every second letter of the string
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
26
● 1 more example
● Like so
● Make sure you add a message in the brackets so you know when you have to provide
input, otherwise you’ll just be given a blank screen to type in.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
27
● Example of input function in a script
●
● Here it takes input and prints it to the console
●
● For even better looking input we can use the format function, or f strings (which are
format strings)
● Example:
●
● And here’s the output:
●
● For extra format strings you just give the input function multiple times, so it will take
multiple inputs.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
28
● Like so
●
● The code in a script
●
● Will give the same output as we've seen.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
29
●
● Floor division is basically diving the number and rounding it down.
● Like the Math.floor() in Java and other languages.
● You can also use the modulo operation, which gives you the remainder of that operation
● Like so. But don’t use underscore as a variable in itself, because it’ll
overwrite the default underscore and you won't be able to use it again, until you change
or reset it.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
30
● The underscore can also be used in loops
●
● Here the underscore will print out all the numbers, or all the times a message needs to
be printed
● The word hello will be printed 10 times because the loop will loop 10 times.
●
● Use dir() to look at what methods and functions we can use on lists
● First method for lists: Append
●
● You can as it says, use the append method to add extra items to the end of a list
● This will append the number 11 to the end of the list
●
● It has now been added
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
31
● Clear will empty the list array, self explanatory.
● Next method: Pop
● If you’re familiar with data algorithms, particularly the stack, the pop method will be
familiar. Pop removes the last item in a list array.
● Conversely push adds an item to the array (but unlike append it adds it at the top).
● Unfortunately push is not a method in the list array methods. But you can use append in
the meantime.
● Next method: Reverse
● Reverse just reverses the items in the list, it doesn’t print anything or create any
additional lists.
● Last method: Sort
●
● Example of sort() method
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
32
● Slicing lists
● Slicing lists is like slicing strings
● You use the index to give you a specific place or item.
● This will give you the 5th number (starting from 0)
●
● Note: The reverse function and using the -1 index aren’t actually the same.
● One changes the list and one doesn’t change the list, it just prints it backwards.
● Note 2: Don’t assign a list to another list (for example l1 to l2) if you’re trying to copy a
list to another list. Because you’d still be using the same list, just in 2 places now.
● And if you change one, you change the other.
● Instead use list slicing and assign that to the new list
●
● Like so
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
33
● Another for loop, this will print out the key along with its value in a neater way
●
● We have pop again
●
● Like you would expect, pop removes a specified key but it doesn’t print the key like it
would in lists, it prints out the value of the removed key.
● Here’s an example
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
34
Lesson 2.12: Python summary
Skills Learned From This Lesson: nothing because it’s a summary
● Here’s a summary of the course
●
● Congratulations on learning Python.
Brought to you by: Develop your team with the fastest growing catalog in the
cybersecurity industry. Enterprise-grade workforce development
management, advanced training features and detailed skill gap and
competency analytics.
35