First lecture¶
Zhanchao Yang
Variables and Date Types¶
message= "Hello Python World"
print(message)
Hello Python World
message
'Hello Python World'
message= "hello world"
message
'hello world'
Avoid Name errors¶
print(mesage)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 print(mesage) NameError: name 'mesage' is not defined
Changing Case in a String with Methods¶
One of the simplest tasks you can do with strings is change the case of the words in a string. Look at the following code, and try to determine what’s happening:
name= 'zhanchao yang'
print(name.title())
Zhanchao Yang
name.capitalize()
'Zhanchao yang'
name.title()
'Zhanchao Yang'
name.upper()
'ZHANCHAO YANG'
name.lower()
'zhanchao yang'
first_name="zhanchao"
last_name="yang"
full_name= f"{first_name} {last_name}"
print(full_name.title())
Zhanchao Yang
message = f"Hello, {full_name.title()}!"
print(message)
Hello, Zhanchao Yang!
full_name="{} {}".format(first_name, last_name)
full_name
'zhanchao yang'
Adding Whitespace to Strings with Tabs or Newlines¶
In programming, whitespace refers to any nonprinting character, such as spaces, tabs, and end-of-line symbols. You can use whitespace to organize your output so it’s easier for users to read. To add a tab to your text, use the character combination \t as shown at:
print("Hello")
print("\tHello")
Hello Hello
print("\t\t\tHello")
Hello
filepath=r'D:\GEOG445'
filepath
'D:\\GEOG445'
print("laguage:\n\tpython\n\tJavascript")
laguage: python 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. whitespace from data that people enter. 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 Zhanchao'
print(favorite_language)
python Zhanchao
print(favorite_language.rstrip())
python Zhanchao
print(favorite_language.lstrip())
python Zhanchao
print(favorite_language.strip())
python Zhanchao
text = 'Extra whitespace can be confusing in your programs.'
text.replace(' ','')
'Extrawhitespacecanbeconfusinginyourprograms.'
favorite_language.replace(' ','')
'pythonZhanchao'
text.replace(' ','-')
'Extra-whitespace-can-be-confusing-in-your-programs.'
Avoiding Syntax Errors with Strings¶
One kind of error that you might see with some regularity is a syntax error. A syntax error occurs when Python doesn’t recognize a section of your pro gram as valid Python code. For example, if you use an apostrophe within single quotes, you’ll produce an error. This happens because Python interprets everything between the first single quote and the apostrophe as a string. It then tries to interpret the rest of the text as Python code, which causes errors.
# double quote, with single quote inside, it will work
# escape sign also work
message='one of python's strength'
Cell In[43], line 1 message='one of python's strength' ^ SyntaxError: invalid syntax
message='one of python\'s strength'
message
"one of python's strength"
message="one of python's strength"
message
"one of python's strength"
Integers¶
You can add ( +), subtract ( -), multiply (*), and divide ( /) integers in Python.
# just as calculator
2 + 3
5
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. Every programming language must be carefully designed to properly manage decimal numbers so numbers behave appropriately no matter where the decimal point appears.
0.1+0.1
0.2
2*0.2
# automatic converted another type of data to another
# integer to float
0.4
2*'50'
# 2 multiplied string
# don't work
'5050'
2* int('50')
# convert string to number
100
Underscores in Numbers¶
When you’re writing long numbers, you can group digits using underscores to make large numbers more readable. When you print a number that was defined using underscores, Python prints only the digits. Python ignores the underscores when storing these kinds of values. Even if you don’t group the digits in threes, the value will still be una ffected. To Python, 1000 is the same as 1_000, which is the same as 10_00. This feature works for integers and floats, but it’s only available in Python 3.6
universe_age=124_000_000
universe_age
124000000
# won't work with comma
a=1,0000
a
(1, 0)
Multiple Assignment¶
You can assign values to more than one variable using just a single line. This can help shorten your programs and make them easier to read; you’ll use this technique most often when initializing a set of numbers.
x = 0
y = 1
x = 3 * y
x
3
# must be paired up
x,y,z=1,2,3
Comments¶
Comments are an extremely useful feature in most programming languages. Everything you’ve written in your programs so far is Python code. 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.
How Do You Write Comments?¶
In Python, the hash mark (#) indicates a comment. Anything following a hash mark in your code is ignored by the Python interpreter. For example:
# y=5
finish¶
References¶
- Matthes, Eric (2022). Python Crash Course, 3rd Edition: A Hands-On, Project-Based Introduction to Programming. No Starch Press. ISBN: 978-1593279288. (Publisher website)
- Dr.Qiusheng Wu online lecture