Python Syntax
Read the text below and do the tasks in red in comments
Python
Syntax
Hello World!
If programming is the act of teaching a computer to
have a conversation with a user, it would be most useful to first teach the
computer how to speak. In Python, this is accomplished with the print statement.
print "Hello,
world!"
print "Water;
there is not a drop of water there! Were Niagara but a cataract of
sand, would you travel your thousand miles to see it?"
A print statement is the easiest way to get your Python program to
communicate with you. Being able to command this communication will be one of
the most valuable tools in your programming toolbox.
Print
Statements
There are two different Python versions. Both Python 2
and Python 3 are used throughout the globe. The most significant difference
between the two is how you write a print statement.
In Python 3, print has parentheses.
print("Hello
World!")
print("Deep into
distant woodlands winds a mazy way, reaching to overlapping spurs of
mountains bathed in their hill-side blue.")
In this course we will be
using Python 2. If you go on to write Python 3 it will be useful to note this
key difference.
1.
Print something using Python
3’s syntax.
Strings
When printing things in Python, we are supplying a
text block that we want to be printed. Text in Python is considered a specific
type of data called a string. A string, so named because they’re a
series of letters, numbers, or symbols connected in order — as if threaded
together by string.
Preview: Docs Loading link
description
Strings can be defined in different ways:
print "This is
a good string"
print 'You can use
single quotes or double quotes for a string'
Above we printed two things that are strings and then
attempted to print two things that are not strings. While double-quotes (“) and
single-quotes (‘) are both acceptable ways to define a string, a string needs
to be opened and closed by the same type of quote mark.
We can combine multiple strings using +, like so:
print "This is
" + "a good
string"
This code will print out “This
is a good string”.
1.
Try adding your name to
the print
statement with the +
operator
so that this Python program prints “Hello [your_name]”
Handling
Errors
As we get more familiar with the Python programming
language, we run into
Preview: Docs Loading link
description errors and exceptions. These are complaints that Python
makes when it doesn’t understand what you want it to do. Everyone runs into
these issues, so it is a good habit to read and understand them. Here are some
common errors that we might run into when printing strings:
print "Mismatched
quotes will cause a SyntaxError'
print Without quotes will cause a NameError
If the quotes are mismatched Python will notice this
and inform you that your code has an error in its syntax because the line ended
(called an EOL) before the double-quote that was supposed to close
the string appeared. The program will abruptly stop running with the following
message:
SyntaxError: EOL while scanning a string literal
This means that a string wasn’t closed, or wasn’t
closed with the same quote-character that started it.
Another issue you might run
into is attempting to create a string without quotes at all. Python treats
words not in quotes as commands, like the print statement.
If it fails to recognize these words as defined (in Python or by your program
elsewhere) Python will complain the code has a NameError. This means
that Python found what it thinks is a command, but doesn’t know what it means
because it’s not defined anywhere.
1.
We’ve written two print
statements
that will raise errors.
Fix the two print
statements to successfully debug the program!
print("How
do you make a hot dog stand?)
print(You take
away its chair!)
1.Print ("Hello" + "Aminat")
ОтветитьУдалить2. Print("You take away make a hot god stand?")
3. Print("You take away its chair!")
Osmanova A.
Удалить1) print ("My name is Magomed")
ОтветитьУдалить2) print("My name is " + "Magomed")
3)
print("How do you make a hot dog stand?")
print("You take away its chair!")
1. print(“I study at DSU“)
ОтветитьУдалить2. print “Hello” + “Khadizha”
3. print(“How do you make a hot dog stand?”)
print(“You take away its chair!”)
saadueva khadizha
1. print(“Сегодня солнечная погода”)
ОтветитьУдалить2. print(“Hello” + “Ashura”)
3. print("How do you make a hot dog stand?")
print You take away its chair!
Ashura
Удалить