Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python3 Learning Notes
- ## by NCJ
- <br />
- <!-- `[TOC]` -->
- ---------------------------------------------------------------------------------------------------------------------------------------------
- ### Chapter 1 Setup
- ```python
- import this
- ```
- 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!
- <br/>
- ### Chapter 2 Data Types
- + 001 variables
- ```python
- msg = "hello"
- print(msg)
- ```
- hello
- + 002 string operations
- ```python
- name = "ada lovelace"
- print(name.title())
- print(name.upper())
- print(name.lower())
- ```
- Ada Lovelace
- ADA LOVELACE
- ada lovelace
- ```python
- new_msg = msg + " " + name
- print ('\t' + new_msg + '\n')
- ```
- hello ada lovelace
- ```python
- msg_1 = ' hiuf yeb w hviu ywe r '
- print(msg_1.rstrip())
- ```
- hiuf yeb w hviu ywe r
- ```python
- msg_1 = msg_1.rstrip()
- print(msg_1.lstrip())
- ```
- hiuf yeb w hviu ywe r
- ```python
- msg_1 = '\t' + msg_1 + '\n'
- print(msg_1)
- msg_1 = msg_1.rstrip()
- print(msg_1)
- msg_1 = msg_1.lstrip()
- print(msg_1)
- ```
- hiuf yeb w hviu ywe r
- hiuf yeb w hviu ywe r
- hiuf yeb w hviu ywe r
- + 003 number operations
- ```python
- 10**6
- ```
- 1000000
- ```python
- 3/2
- ```
- 1.5
- ```python
- 3//2
- ```
- 1
- ```python
- 0.1*3
- ```
- 0.30000000000000004
- *floats' problems still exists in python3, but it's common in any language due to how computers save these floats*
- + 004 number&string
- ```python
- num_test_1 = 666
- print(num_test_1)
- # print('huvife' + num_test_1 + 'gbtehgbe')
- # error version
- print('huvife' + str(num_test_1) + 'gbtehgbe')
- # or
- num_test_1 = '666'
- print('huvife' + num_test_1 + 'gbtehgbe')
- ```
- 666
- huvife666gbtehgbe
- huvife666gbtehgbe
- <br/>
- ### Chapter 3 List
- *There is no array in Python!!!*
- + 001 Introduction to Lists
- ```python
- arr = ['vfervewr','gftewgwter','gtewgewwe','gtwgw']
- print(arr)
- print(arr[0].title())
- print(arr[-1]) #see? minus is not only available, but useful!
- print(arr[-3])
- message = "vfsvegeerwsgetrw " + arr[0] + " vbgfdbgfrdsbg"
- print(message)
- # message = "vfsvegeerwsgetrw " + arr + " vbgfdbgfrdsbg"
- # print(message)
- # error version
- ```
- ['vfervewr', 'gftewgwter', 'gtewgewwe', 'gtwgw']
- Vfervewr
- gtwgw
- gftewgwter
- vfsvegeerwsgetrw vfervewr vbgfdbgfrdsbg
-   *Again, thers is no array in Python! So don't follow my mistake! (I used 'arr' to name a list, because I thought they were the same!)*
- + 002 Modifying Lists
- ```python
- arr[0] = 'cool'
- print(arr)
- ```
- ['cool', 'gftewgwter', 'gtewgewwe', 'gtwgw']
- + 003 Add Elements
- ```python
- arr.append('new one')
- print(arr)
- ```
- ['cool', 'gftewgwter', 'gtewgewwe', 'gtwgw', 'new one']
-   You can even create an empty list and add elements to it later!
- ```python
- motor = []
- motor.append('honda')
- motor.append('yamaha')
- motor.append('suzuki')
- print(motor)
- ```
- ['honda', 'yamaha', 'suzuki']
- + 004 Insert Elements
- ```python
- motor.insert(0,'ducati')
- print(motor)
- ```
- ['ducati', 'honda', 'yamaha', 'suzuki']
- + 005 Delete Elements
- ```python
- print(motor)
- del motor[1]
- print(motor)
- ```
- ['ducati', 'honda', 'yamaha', 'suzuki']
- ['ducati', 'yamaha', 'suzuki']
- + 006 Pop Elements
- ```python
- print(motor)
- poped_motor = motor.pop()
- print(motor)
- print(poped_motor)
- ```
- ['ducati', 'yamaha', 'suzuki']
- ['ducati', 'yamaha']
- suzuki
-   This way, you can still use the element you want to delete for the last time.
-   This is useful! Watch this example!
- ```python
- motor = ['ducati', 'honda', 'yamaha', 'suzuki']
- last_owned = motor.pop()
- print("The last motorcycle I owned was a " + last_owned.title() + ".")
- print(motor)
- ```
- The last motorcycle I owned was a Suzuki.
- ['ducati', 'honda', 'yamaha']
-   And pop can be used to pop any element!
- ```python
- motor = ['ducati', 'honda', 'yamaha', 'suzuki']
- first_owned = motor.pop(0)
- print("The first motorcycle I owned was a " + first_owned.title() + ".")
- print(motor)
- ```
- The first motorcycle I owned was a Ducati.
- ['honda', 'yamaha', 'suzuki']
- + 007 Remove Elements According to Its Value
- ```python
- motor = ['ducati', 'honda', 'yamaha', 'suzuki']
- motor.remove('ducati')
- print(motor)
- ```
- ['honda', 'yamaha', 'suzuki']
- ```python
- motor = ['ducati', 'honda', 'yamaha', 'suzuki']
- too_expensive = 'ducati'
- motor.remove(too_expensive)
- print(motor)
- print("\nA " + too_expensive.title() + " is too expensive for me.")
- ```
- ['honda', 'yamaha', 'suzuki']
- A Ducati is too expensive for me.
Add Comment
Please, Sign In to add comment