Computer Science Pathway, Mrs. Williams



Topic 0: print commandA computer program is a series of instructions, written in words, numbers and symbols.Here is an analysis of this first program:print is the name of a Python command, which sends messages to output.The parentheses () after the print command are used to contain what you want to print.The quote marks " " are used as a container for the text Hello, World! inside. Without quotes, Python would think that Hello was meant to be a command. This would be cause an error since no such command exists.Any characters, text, words, number or symbols between quote marks “ “ are a stringExample: print(“Hello, World”) output:___________________________________Topic 1: VariableVariables act as "storage locations" for data in a program. A variable name can be whatever you decide the name to be.?the variable name??= ?the value you want to store?Example:myLuckyNumber = 13Output:_________________________________print(myLuckyNumber + 1)print(myLuckyNumber)Example:x = 10Output:___________x = x + xx = x – 5print(x)Topic 1E: ErrorsA syntax error happens when Python can't understand what you are saying. A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions.In English, a syntax error would be like the sentencePlease cat dog monkey.The grammar of this sentence does not make sense. From the perspective of English grammar, it is missing a verb (action). You cannot understand what you are being asked to do. Syntax means the same thing as grammar.In English, a run-time error would be like the sentencePlease eat the piano.Example: name = "Jim Is this a Syntax or Run-time error? _________________Example: callMe = "Maybe"print(callme) Is this a Syntax or Run-time error? _________________Example:print("Here is some text")Is this a Syntax or Run-time error?__________________print(1*0Topic 2: Comments and Escape SequencesIn Python, any line of instructions containing the # symbol ("pound sign" or "hash") denotes the start of a comment. The rest of the line will be ignored when the program is run.Example: print(“I am a great programmer”) #this is a comment Output: __________________________________You are allowed to start and end a string literal with single quotes (also known as apostrophes), like 'blah blah'. Then, double quotes can go in between, such as 'I said "Wow!" to him.'You can put a backslash character followed by a quote (\" or \'). This is called an escape sequence and Python will remove the backslash, and put just the quote in the ic 3: TypeString value (str)Integer value (int)Float value (float)Example:a = float(inputStr) * float(inputStr)Output _________________p = int(a/100)print(p)Topic 4: InputYou should use the input() function to actually obtain the next line of input from the user. Think about Eliza. Create code that will allow your user to speak to the program.Example: x = input()Output _________________print(x)print(x)Example: x = int(input(“What is your favorite number?”))Output _________________print(“Your favorite number is, x,)Arithmetic ExpressionsString LiteralsYou can enclose strings in single quotes, double quotes, or sets of three double quotes or three single quotes. The last notation is useful for a string containing multiple lines of text. Character values are single-character strings. The \ character is used to escape nongraphic characters such as the newline (\n)and the tab (\t),or the \ character itself. What would be the output from the code below:print("Using double quotes")Output: ________________________print(’Using single quotes’) Output: ________________________print("Mentioning the word ’Python’ by quoting it") Output: ________________________print("Embedding a\nline break with \\n") Output: ________________________print("""Embedding a line break with triple quotes""")Output: ________________________Topic 5: Indexing & Slicing a String>>> word = "YouAreAmazing" >>> >>>print(word [5])>>>print(word [9])>>>print(word[2:7])>>> print(word[6:6]) >>> print(word[4:2]) >>> print(word[5:11]) >>> print(word[8:13]) >>> print(word[-7:-2]) >>> print(word[-9:8]) >>> print(word[-3:-7])>>> print(word[8:]) >>> print(word[-6:]) >>> print(word[:6]) >>> print(word[: : -1]) Topic 6: If StatementThe new ability in this lesson is the?if statement, which allows us to perform actions only when certain conditions are met, and skip lines otherwise. For example, let's change our program to output an extra message when someone's age is large enough. We will add two lines: if age > 65:?(meaning, "if age is greater than 65") and ? print('Enjoy retirement!').We call the thing being checked the condition, and the statements that are dependent on whether the condition holds true the body. The body has to be indented which means there are extra spaces at the start of the line (like in the example above). The body can actually contain more than one statement: to do this, write several indented lines. Here's an example.Example: birthYear = 1928currentYear = 2013age = currentYear - birthYearif age > 65: print('Enjoy retirement!') print('You turn 100 in', 100+birthYear)else: print('You are', age, 'years old')output if you were born in 1981______________output if you were born in 1908 _____________Example of “More Decisions” meal = input("breakfast, lunch, or dinner? ")if meal == "breakfast":print("How about some avocado on toast?")elif meal == "lunch":print("Would you like a burrito today?")elif meal == "dinner":print("Let's have a salad!")else: print("Invalid option")output if you input breakfast _________________output if you input brunch ___________________ ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download