PYTHON DICTIONARY
CommentsData typesVariablesIf ElseIf Elif ElseFunctionsFor loopWhile loopData structuresString slicing/indexingUser defined functionLocal/Global variableValidation and TryMethodsFile I/OPseudocodeModules |
What is a data string slicing or indexing?
Python challenge - String slicingEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
How do I display the middle "d" in the word "MAdAM"? The code above allows you to slice it out and display it. Pay attention to the index values [2]:[3]. This is providing more detail to exactly how the index is labelled. Much like the example of "banana" above, the labels actually exist between each letter. So, in this example, the letter "d" exists between index location 2 and index location 3.
More string slicingRun the code below and see what it does
Changes the names (and rerun) Change the [0] to [1], guess what will happen then rerun the code Change the [0] to [0:2], guess what will happen then rerun the code Edit the code to allow anyone to enter their name and it displays their initials Concatenation explainedQuestion: When is an add sign + not an add sign?
Answer: When you are NOT working with numbers! Explanation: in line 1 and 2 below, "int" is being used (casted), this is short for integer (a whole number) In line 4, the numbers are being added together with the + However, in line 6 and 7 "Str" (Strings) are being used (casted) In line 10, the strings are being concatenated (squashed together) This is an example of polymorphism (the + sign behaving in two different ways, adding or squashing!) Python challenge - Slicing and loopsEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
What do you thin the following code will do? Try it out. Now change the "c" to a "d". What if you change the [0] to [1] and change the "c" to an "a".
Test your understanding
Loading String slicing
The len function - len()Before you run the code below, know one thing. To "spam" a keyboard means to tap your fingers as quickly as you can on any letters or numbers (apart from enter). Who doesn't love to spam a keyboard?
More len() functionWhat do you think the following code will do? Try it out. Now add a new date (pretend that France won the world cup in 1994 by adding the date to list on line 3 ) What is output down when you run he code?
Test your understanding
Loading len() function
The .find() methodWhat do you think the following code will do? Try it out. Now change the target to "am" and run it again. What is output down when you run the code?
Test your understanding
Loading .find() method
The .isalpha() methodWhat do you think the following code will do? Try it out. Now delete the numbers and set name to "Barcelona". run it again
The .isdigit() methodWhat do you think the following code will do? Try it out. Now delete the random letter hidden between the zeros. Run it again.
|