5_6149975860259591061
5_6149975860259591061
● When you run the file hello_world.py, the ending .py indicates
that the file is a Python program.
● Your editor then runs the file through the Python interpreter
print(message)
print(message)
print(message)
You can change the value of a variable in your program at any time, and Python will always keep
track of its current value.
Naming and Using Variables
● Variable names can contain only letters, numbers, and underscores. For instance,
you can call a variable message_1 but not 1_message.
● Spaces are not allowed in variable names, but underscores can be used to separate
words in variable names.
● greeting_message works, but greeting message will cause errors.
● Avoid using Python keywords and function names as variable names such as def,
else, elif, continue, etc.
● Variable names should be short but descriptive. For example, name is better than n,
student_name is better than s_n, and name_length is better than
length_of_persons_name
Name Errors When Using Variables
● We’ll write some code that generates an error on purpose.
print(mesage)
File "hello_world.py", line 2, in <module> The interpreter shows this line to help us spot
print(mesage) the error quickly
NameError: name 'mesage' is not defined tells us what kind of error it found
Strings
● A string is simply a series of characters. Anything inside quotes is considered a string
in Python, and you can use single or double quotes around your strings like this
"This is a string."
'This is also a string.'
● This flexibility allows you to use quotes and apostrophes within your strings:
Ada Lovelace
● The method title() appears after the variable in the print() statement.
● A method is an action that Python can perform on a piece of data. The dot (.) after
name in name.title() tells Python to make the title() method act on the variable name.
● Every method is followed by a set of parentheses, because methods often need
additional information to do their work.
● The title() function doesn’t need any additional information, so its parentheses are
empty.
Changing Case in a String with Methods
name ="Ada Lovelace"
print(name.upper())
print(name.lower())
ADA LOVELACE
ada lovelace
print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
Stripping Whitespace
● Extra whitespace can be confusing in your programs. To programmers 'python' and
'python ' look pretty much the same.
● But to a program, they are two different strings. Python detects the extra space in
'python ' and considers it significant unless you tell it otherwise.
● It’s important to think about whitespace, because often you’ll want to compare two
strings to determine whether they are the same.
● For example, one important instance might involve checking people’s usernames
when they log in to a website.
● Fortunately, Python makes it easy to eliminate extraneous whitespace from data that
people enter.
Stripping Whitespace
● Python can look for extra whitespace on the right and left sides of a string. To ensure
that no whitespace exists at the right end of a string, use the rstrip() method.
favorite_language = 'python '
print(favorite_language.rstrip())
● you can see the space at the end of the value v. When the rstrip() method acts on
the variable favorite_language, this extra space is removed. But it is a temporary
solution.
● To remove the whitespace from the string permanently, you have to store the
stripped value back into the variable:
>>>favorite_language = 'python '
>>>favorite_language = favorite_language.rstrip()
>>>favorite_language
'python'
Stripping Whitespace
● You can also strip whitespace from the left side of a string using the lstrip() method
or strip whitespace from both sides at once using strip():
>>> favorite_language = ' python '
>>> favorite_language.rstrip()
' python'
>>> favorite_language.lstrip()
'python '
>>> favorite_language.strip()
'python'
● Experimenting with these stripping functions can help you become familiar with
manipulating strings.
● In the real world, these stripping functions are used most often to clean up user input
before it’s stored in a program.
Syntax Errors with Strings
● A syntax error occurs when Python doesn’t recognize a section of your program as
valid Python code.
● If you use an apostrophe within single quotes, you’ll produce an error.
message = 'One of Python's strengths is its diverse community.'
print(message)
● Experimenting with these stripping functions can help you become familiar with
manipulating strings.
● In the real world, these stripping functions are used most often to clean up user input
before it’s stored in a program.
Numbers (i.e., Integers)
● Numbers are used quite often in programming to keep score in games, represent
data in visualizations, store information in web applications
● You can add (+), subtract (-), multiply (*), and divide (/) integers in Python.
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5
● Python uses two multiplication symbols to represent exponents
>>> 3 ** 2
9
>>> 3 ** 3
27
Operator Precedence
● Python supports the order of operations too, so you can use multiple operations in
one expression.
● You can also use parentheses to modify the order of operations so Python can
evaluate your expression in the order you specify.
>>> 2 + 3*4
14
>>> (2 + 3) * 4
20
Floats
● Python calls any number with a decimal point a float. This term is used in most
programming languages, and it refers to the fact that a decimal point can appear at
any position in a number.
>>> 0.1 + 0.1
0.2
>>> 2 * 0.1
0.2
● But be aware that you can sometimes get an arbitrary number of decimal places in
your answer:
>>> 0.2 + 0.1
0.30000000000000004
Errors with the str() Function
● Sometimes you will want to use a variable’s value within a message
age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
● But if you run this code, you’ll see that it generates an error
● This is a type error. It means Python can’t recognize the kind of information you’re
using.
Errors with the str() Function
● When you use integers within strings like this, you need to specify explicitly that you
want Python to use the integer as a string of characters.
●
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
Integers in Python 2
● Python 2 returns a slightly different result when you divide two integers:
>>> python2.7
>>> 3 / 2
1
>>> 3.0 / 2
1.5
Comments
● Comments are an extremely useful feature in most programming languages.
● As your programs become longer and more complicated, you should add notes
within your programs that describe your overall approach to the problem you’re
solving.
● A comment allows you to write notes in English within your programs.
● The hash mark (#) indicates a comment
# Say hello to everyone.
print("Hello Python people!")
● Python ignores the first line and executes the second line.