Frawley: Chapter 4 Notes

Chapter 4 Notes: This was an amazing review for me, essentially a complete introduction to python and its syntax and how to use it. This was genuinely the best python tutorial that I have read just because of how concise and to the point it was. Iā€™ve read about two different intermediate-level python books and they didnā€™t compare to the examples and just the great layout the author put forth. Anyone could read through this chapter a few times and receive a pretty intermediate-level understanding of python. Genuinely you can mentally program in your head once learning the syntax. For me, this has helped me visualize projects easier and how I would accomplish them. I spent at least two hours reading this chapter several times to absorb the massive amount of information thrown at you.Ā 

It helped me ponder on a semester project. I’m possibly thinking about attempting to automate a mapping sequence and run data extraction by teaching the program how to locate and measure each feature. On what, I am not exactly sure yet.Ā 

 

What are the main data types in python? Which data types are sequences? Which data types are mutable, which are immutable?Ā 

Strings, tuples, lists, dictionaries, boolean expressions, integers, float. Strings, tuples, and numbers are immutable. Lists and dictionaries are mutable, meaning they can be modified.Ā 

 

Describe the difference between true division and floor division.Ā 

Floor division divides with no remainder, while true division divides into a whole number with a leftover remainder.Ā 

 

Explain what the dynamic assignment of a variable means.Ā 

Dynamic assignment of variables occurs after you assign a value to it. Specifically, y = 6 is an integer, y =Ā  10.33 is a float, and y = ā€œMarkā€ is a string. The variable you assigned the value to becomes that exact data type you applied to it.Ā 

 

Give examples of variable names and why they are good.Ā 

Counter, field5, my_file. Underscores are great for creating a unique variable name. All lowercase or including a number helps differentiate the variables from the rest of the code to avoid confusion.Ā 

 

Whatā€™s the difference between an expression and a statement in python, give examples.

An expression is a value with no variable assignment. For example, 3 * 10 and the console will calculate it and return 30. A statement is a function that assigns a value to a variable. For example, x = 40 * 3. The x itself is not a value, it is just assigned one, and to display the result you need to incorporate the print() function to display the value. Print(x) and it would return 120.Ā 

 

Explain the use of quotation marks in python

Quotation marks assign a collection of characters as a string, which is designated to a variable.Ā 

 

What are Unicode strings?Ā 

Strings are essentially plain text, and Unicode allows that plain text to be converted into other languages, and with Unicode you can use different letters from other languages by using their designated ASCII code.Ā 

 

Name five built in functions in python and provide an example of each oneā€™s use.Ā 

Print(x) : Prints a result from a function. Print (name) – Mark

pow(x,y) : First number is multiplied by a ā€˜powerā€™Ā  of the later value pow(2,3) – 8Ā 

string (x) : Can convert an integer into a string to be able to display the integer in a normal lineĀ 

of string characters. str(10) = ā€œ10ā€

abs(x) : Returns absolute value of a number. abs(-8) – 8

float(x) : converts a string or number into a float. float(ā€œ8.0ā€) – 10.0Ā 

 

What is dot notation?Ā 

Shows that method belongs to the object. What comes after the dot belongs to what came before it.Ā 

 

Describe the use of forward and reverse indexing sequences. Provide an example of slicing strings and lists.Ā 

Forward indexing is used to extract or display certain characters in a string, list, dictionary, or tuple, by a specific number correlated to the character. For example, indexing starts at 0, so indexing a string can retrieve a certain number or letter. Mystring = ā€œMark is the bestā€Ā 

Mystring[0] would return ā€œMā€. Slicing strings and lists can be used to take a chunk of letters or values from the string or list. This is done by using the index numbers and a colon to separate the first and last values you want to return from the string or list.Ā 

 

Name three methods of string objects in python, provide an example of each oneā€™s use.Ā 

.join(x) – Joins values in a list into one string.Ā 

Leela_list = [ ā€˜sheā€™ , ā€˜needsā€™ , ā€˜dogā€™ , ā€˜foodā€™]Ā 

Leelastring = ā€œā€

leela_list.join(leelastring) = ā€˜she needs dog foodā€™Ā 

 

.split(x) – Can turn a string into a list by splitting each word into substrings

bestdog = ā€œLeela is the best dog.ā€

Bestdog.split(ā€œ ā€)Ā  = [ā€˜Leelaā€™ , ā€˜isā€™ , ā€˜theā€™ , ā€˜bestā€™, ā€˜dogā€™]Ā 

 

.replace(x) – replace all occurrences of a specific substring with another substring

 

myhouse = ā€œ609 loganā€

myhouse.replace(ā€œ609ā€, ā€œTorontoā€)

 

What are some key similarities between lists and tuples?Ā 

They can both hold multiple values, except tuples are immutable.Ā 

 

Describe the use of boolean values to evaluate a condition.Ā 

Boolean values are true/false values that can differentiate if a value is there or not, essentially a yes or no.Ā 

X = 3Ā 

X == 8

Returns falseĀ 

Describe the key steps to create a dictionary and add new items to the dictionary.Ā 

The first step to creating a dictionary is to assign a variable name to it and set it equal to empty braces. For example,

Ā teams = {}

To add to a dictionary, you include brackets to the variable with a pair of quotation marks inside and set it equal to the complete string.Ā 

teams[ā€œCHIā€] = ā€œChicago Blackhawksā€Ā 

And to add to the dictionary you continue to use the same syntax.Ā 

 

Why should backslashes be avoided when writing paths in python?Ā 

Because a single backslash is used to denote a new line of text in strings.Ā 

 

Describe how branching is implemented in python.Ā 

Branching is implemented with if statements, telling the compiler that it should take one path or another depending on the returned value.Ā 

 

What is a compound statement in python?Ā 

A compound statement is a block of code expected to run together as a block, which is denoted by indentation.Ā 

 

What are the two main looping structures in python? Describe each.Ā 

While loops check for the value of a variable and require an exit statement.Ā 

For loops repeat a block of code for each element in that sequenceĀ 

 

What approaches can be used to split long lines of code into multiple lines and improve readability?Ā 

Enter a backslash in a defined string within quotation marks.Ā 

Implied line continuation (), [], {}.Ā 

Hanging indentationĀ 

 

Why should you add comments to your scripts?Ā 

To help separate parts of the code into blocks

Designate certain code with comments to know that this block is performing a particular function within the script.