Workbook 1 Well come to the Pythoning! Type this code import this and you can se

WRITE MY ESSAY

Workbook 1
Well come to the Pythoning!
Type this code import this and you can see the Zen of Python
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Python I/O
Computer programs consist of commands, each command instructs the computer to take some action. A computer executes these commands one by one. Among other things, commands can be used to perform calculations, compare things in the computer’s memory, cause changes in how the program functions, relay messages or ask for information from the program’s user.
Let’s begin programming by getting familiar with the print() command, which prints text. In this context, printing essentially means that the program will show some text on the screen.
The following program will print the line “Hi there!”:
print(“Hi there!”)
When the program is run, it produces this:
Hi there!The program will not work unless the code is written exactly as it is above. For example, trying to run the print command without the quotation marks, like so
print(Hi there!)
will not print out the message, but instead causes an error:
In summary, if you want to print text, the text must all be encased in quotation marks or Python will not interpret it correctly.
Exercise 1 (1 point)
Please write a program which prints out an emoticon: 🙂
A program of multiple commands
Multiple commands written in succession will be executed in order from first to last. For example this program
print(“Welcome to Introduction to Programming!”)
print(“First we will practice using the print command.”)
print(“This program prints three lines of text on the screen.”)
prints the following lines on the screen:
Welcome to Introduction to Programming!
First we will practice using the print command.
This program prints three lines of text on the screen.
Exercise 2 (1 point)
The Mount Rushmore National Memorial is a national memorial centered on a colossal sculpture carved into the granite face of Mount Rushmore (Lakota: Tȟuŋkášila Šákpe, or Six Grandfathers) in the Black Hills near Keystone, South Dakota, United States.
This program is supposed to print the names of American presidents (left to right), but it doesn’t work quite right yet. Please fix the program so that the names are printed in the correct order.
print(“George Washington”)
print(“Theodore Roosevelt”)
print(“Abraham Lincoln”)
print(“Thomas Jefferson”)
Exercise 3 (1 point)
Please write a program which prints out the following lines exactly as they are written here, punctuation and all:
Row, row, row your boat,
Gently down the stream.
Merrily, merrily, merrily, merrily,
Life is but a dream.
Exercise 4 (1 point)
The popular TV series Lost used the number sequence 4 8 15 16 23 42, which brought good luck to the characters and helped them hit the lottery jackpot. Write a program that prints the given sequence of numbers with one space in between.
Note. The plain text ‘4 8 15 16 23 42’ cannot be used. Instead, use the print() command’s ability to print multiple arguments, separated by commas.
Exercise 5 (1 point)
Modify the previous program so that each number in the sequence 4 8 15 16 23 42 is printed on its own line.
Exercise 6 (1 point)
Write a program that prints the following triangle made up of asterisks (*):
*
**
***
****
*****
******
*******
Arithmetic operations
You can also put arithmetic expressions inside a print command. Running it will then print out the result. For example, the following program
print(2 + 5)
print(3 * 3)
print(2 + 2 * 10)
prints out these lines:
7
9
22
Notice the lack of quotation marks around the arithmetic operations above. Quotation marks are used to signify strings. In the context of programming, strings are sequences of characters. They can consist of letters, numbers and any other types of characters, such as punctuation.
Strings aren’t just words as we commonly understand them, but instead a single string can be as long as multiple complete sentences. Strings are usually printed out exactly as they are written. Thus, the following two commands produce two quite different results:
print(2 + 2 * 10)
print(“2 + 2 * 10”)
This program prints out:
22
2 + 2 * 10
With the second line of code, Python does not calculate the result of the expression, but instead prints out the expression itself, as a string. So, strings are printed out just as they are written, without any reference to their contents.
Commenting
Any line beginning with the pound sign #, also known as a hash or a number sign, is a comment. This means that any text on that line following the # symbol will not in any way affect how the program functions. Python will simply ignore it.
Comments are used for explaining how a program works, to both the programmer themselves, and others reading the program code. In this program a comment explains the calculation performed in the code:
print(“Hours in a year:”)
# there are 365 days in a year and 24 hours in each day
print(365*24)
When the program is run, the comment will not be visible to the user:
Hours in a year:
8760
Short comments can also be added to the end of a line:
print(“Hours in a year:”)
print(365*24) # 365 days, 24 hours in each day
Variables in print() command
As you noticed, we can use variables within the print() command. To do this, we simply supply the variable name without quotes as an argument inside the parentheses.
city = ‘New York’
print(city, ‘is my city!’)
This prints out:
New York is my city!
We can use several variables in the print() command. Just remember that the arguments to the print() command need to be separated by commas.
name = ‘Alex’
city = ‘Rockford’
print(‘My name is’, name, ‘.’, city, ‘is my city!’)
My name is Alex . Rockford is my city!
Naturally, variable values can vary (be changed) though reassigned. Try this:
print(‘What is your name’)
name = input()
print(‘Hi,’, name)
name = ‘Chris’
print(‘Hello,’, name)
The assignment operator (=) gives a variable a value, regardless of whether the variable was previously used. You can change a variable’s value by writing another assignment statement. Once we have a variable, we can do whatever we want with it, such as assigning it to another variable or changing its value.
name1 = ‘Brooklyn’
name2 = name1
name1 = ‘Jace’
print(name1)
print(name2)
this prints out:
Jace
Brooklyn
Exercise 7 (1 point)
Try this
first = input()
second = input()
print(‘I am’, first, ‘and’, second)
Enter for the first “Cool” and for the second “I know it”
Exercise 8 (1 point)
Try this
name_1 = input() # waiting for the first person’s name
print(‘Hi, ‘, name_1, ‘!’) # output for the first
name_2 = input() # waiting for the second person’s name
print(‘Hello, ‘, name_2, ‘.’) # output for the second
Enter for the first “Liam” and for the second “Ted”
Exercise 9 (2 points)
Write a program that takes three lines through standard input (the input() command) and then prints them in reverse order, each on a separate line.
For example, if the program input is given the following lines:
Hello
it's
me
then it prints out
me
it's
Hello
The Plus (+) Operator
In Python, the plus (+) operator allows you to concatenate (join) two or more strings.
For example:
print(“Hello,” + “world!”)
Hello,world!
Since the plus (+) operator concatenates the strings as they are, they’ll be squished together unless you explicitly include a leading space as part of the print statement:
print(“Hello,” + ” ” + “world!”)
# Hello, world!
print(“Hello, ” + “world!”)
# Hello, world!
print(“Hello,” + ” world!”)
# Hello, world!
Plus (+) act as the concatenation operator if operands are strings otherwise it acts as mathematical plus operator.
Whereas, the comma in the print command is used to separate arguments (variables or expressions). In case of strings , and +in print work almost the same, but fundamentally they are different: + creates new object from operands whereas the comma has print use the objects as is without creating new ones.
Try these to see the differnces:
print(‘this’,‘and’,‘that’)
print(‘this’+‘and’+‘that’)
var1=‘Hello’
var2=‘!!’
print(var1,var2)
print(var1+var2)
print(2,6)
print(2+6)
Exercise 10 (2 points)
Please write a program which asks for the user’s name and address.
The program should also print out the given information, for example: Steve Sanders, 91 Station Road, London EC05 6AW Given name: Steve
Family name: Sanders
Street address: 91 Station Road
City and postal code: London EC05 6AW
Steve Sanders
91 Station Road
London EC05 6AW
Exercise 11 (2 points)
Please write a program which prints out the following story. The user gives a name and a year , which should be inserted into the printout.
An example for Mary, 1572 Please type in a name: Mary
Please type in a year: 1572
Mary is a valiant knight, born in the year 1572. One morning Mary woke up to an awful racket: a dragon was approaching the village. Only Mary could save the village's residents.
The relevant parts in the story should change according to the input given by the user.

WRITE MY ESSAY

Leave a Comment