Keep in mind
- 1.Python is case sensitive.
- 2.Spacing is important.
- 3.Use error messages to help you learn.
目录
Lesson 2: Data Types and Operators
Lesson 2: Data Types and Operators
- Data Types: Integers, Floats, Booleans, Strings, Lists, Tuples, Sets, Dictionaries
- Operators: Arithmetic, Assignment, Comparison, Logical, Membership, Identity
- Built-In Functions, Compound Data Structures, Type Conversion
- Whitespace and Style Guidelines
+
Addition-
Subtraction*
Multiplication/
Division%
Mod (the remainder after dividing)**
Exponentiation (note that^
does not do this operation, as you might have seen in other languages)//
Divides and rounds down to the nearest integer- Bitwise operators are special operators in Python that you can learn more about https://wiki.python.org/moin/BitwiseOperatorsif you'd like.
03. Quiz: Arithmetic Operators
04. Solution: Arithmetic Operators
05. Variables and Assignment Operators
mv_population = 74728
Here mv_population
is a variable, which holds the value of 74728
. This assigns the item on the right to the name on the left, which is actually a little different than mathematical equality, as 74728
does not hold the value of mv_population
.
x = 3
y = 4
z = 5
x, y, z = 3, 4, 5
You can’t use reserved words or built-in identifiers
my_height = 58
my_lat = 40
my_long = 105
my height = 58 ❎
MYLONG = 40 ❎
MyLat = 105 ❎
The pythonic way to name variables is to use all lowercase letters and underscores to separate words.
You can find some practice with much of what we have already covered :https://www.programiz.com/python-programming/operators
06. Quiz: Variables and Assignment Operators
4.445e8 is equal to 4.445 * 10 ** 8 which is equal to 444500000.0.
07. Solution: Variables and Assignment Operators
- int - for integer values
- float - for decimal or floating point values
x = int(4.7) # x is now an integer 4
y = float(4) # y is now a float of 4.0
>>> print(type(x))
int
>>> print(type(y))
float
print(0.1 + 0.1 + 0.1)
0.30000000000000004
print(0.1 + 0.1 + 0.1)
False
Because the float, or approximation, for 0.1 is actually slightly more than 0.1, when we add several of them together we can see the difference between the mathematically correct answer and the one that Python creates.
https://docs.python.org/3/tutorial/floatingpoint.html
Python Developer's Guide: https://www.python.org/dev/peps/pep-0008/
In general, there are two types of errors to look out for https://docs.python.org/3/tutorial/errors.html
- Exceptions
- Syntax
10. Booleans, Comparison Operators, and Logical Operators
- True - 1
- False - 0
- < > <= >= == !=
- and or not
11. Quiz: Booleans, Comparison Operators, and Logical Operators
12. Solution: Booleans, Comparison and Logical Operators
>>> my_string = 'this is a string!'
>>> my_string2 = "this is also a string!!!"
>>> this_string = 'Simon\'s skateboard is in the garage.'
>>> print(this_string)
Simon's skateboard is in the garage.
>>> first_word = 'Hello'
>>> second_word = 'There'
>>> print(first_word + second_word)
HelloThere
>>> print(first_word + ' ' + second_word)
Hello There
>>> print(first_word * 5)
HelloHelloHelloHelloHello
>>> print(len(first_word))
5
>>> first_word[0]
H
>>> first_word[1]
e
len()
is a built-in Python function that returns the length of an object, like a string. The length of a string is the number of characters in the string. This will always be an integer.
print(len("ababa") / len("ab"))
2.5
17. Quiz: Type and Type Conversion
18. Solution: Type and Type Conversion
21. Another String Method - Split
22. Lists and Membership Operators
23. Quiz: Lists and Membership Operators
24. Solution: List and Membership Operators
31. Dictionaries and Identity Operators
32. Quiz: Dictionaries and Identity Operators
33. Solution: Dictionaries and Identity Operators
34. Quiz: More With Dictionaries
36. Quiz: Compound Data Structures
37. Solution: Compound Data Structions
Lesson 3: Control Flow
- Conditional Statements
- Boolean Expressions
- For and While Loops
- Break and Continue
- Zip and Enumerate
- List Comprehensions
Lesson 4: Functions
- Defining Functions
- Variable Scope
- Documentation
- Lambda Expressions
- Iterators and Generators
Lesson 5: Scripting
- Python Installation and Environment Setup
- Running and Editing Python Scripts
- Interacting with User Input
- Handling Exceptions
- Reading and Writing Files
- Importing Local, Standard, and Third-Party Modules
- Experimenting with an Interpreter