day04 data type (2)
Today's Content
- list
- tuple
Content review and supplement
-
Computer Foundation
-
Hardware: CPU/memory/hard disk/motherboard/network card
-
Operating system:
- linux (free / open source)
- centos
- ubuntu
- redhat
- windows
- mac
- linux (free / open source)
-
Interpreter/compiler
-
Supplementary: Compiled and Explanatory Languages?
# Compiler: After the code is written, the compiler turns it into another file, which is then handed over to the computer for execution. # Interpretative: After writing the code to the interpreter, the interpreter will execute from the top to the next line of code: while interpreting, while executing. [Real-time translation]
-
-
Software (applications)
-
-
Environmental Installation
- python interpreter
- py2
- py3
- Development tools: pycharm (recommendation)/text editor
- python interpreter
-
Python grammar
-
Interpreter path: hello.py
#!/usr/bin/env python print('Hello')
linux system application:
- Give file executable permissions
- ./hello.py
-
Code
#!/usr/bin/env python # -*- coding:utf-8 -*- print('Hello')
- Coding type
- acsii
- unicode
- utf-8 / utf-16
- gbk/gb2312
- Chinese Representation
- Utf-8:3 bytes
- gbk: 2 bytes
- python default interpreter encoding
- py3: utf-8
- py2: ascii
- Coding type
-
Input and output
- py2:
- Input: raw_input
- Output: print ""
- py3:
- Input:input
- Output: print()
- py2:
-
data type
-
int
- There are int/long in py2 and int in py3.
- Forced Conversion: int(''76')
- Division: py2 (one more line of code) and py3 (normal)
-
bool
- True/False (other languages: true/false)
- Other types of False in particular: 0 and ""
-
str
-
Unique function
-
upper/lower
-
replace
-
strip/lstrip/rstrip
-
isdigit
-
split / rsplit
-
Supplement:
-
startswith / endswith
name = 'alex' # Determine if al has started """ # Mode 1: flag = name.startswith('al') print(flag) """ """ # Mode 2: val = name[0:2] if val == 'al': print('In order to al Start') else: print('No') """
-
format
name = "My name is{0},Age:{1}".format('Old boy',73) print(name)
-
encode
name = 'Li Jie' # After the interpreter reads the memory, it stores it according to unicode encoding: 8 bytes. v1 = name.encode('utf-8') print(v1) v2 = name.encode('gbk') print(v2)
-
join
name = 'alex' # a_l_e_x result = "**".join(name) # Loop each element and add a connector between the elements. print(result)
-
-
-
Public function
-
Index, get a character.
-
Slice to get a string (subsequence).
-
step
name = 'alex' # val = name[0:-1:2] # val = name[1:-1:2] # val = name[1::2] # val = name[::2] # val = name[-1:0:-2] # print(val) # Written questions: Please invert the string. val = name[::-1] print(val)
-
Length, get character length.
-
for loop
name = 'alex' for item in name: print(item)
name = 'alex' for item in name: print(item) break print('123')
name = 'alex' for item in name: print(item) continue print('123')
# Exercises # 1. for loop printing each element of "alex": for > while # 2. Please print: 1 - 10 """ for i in range(1,11): # [1,2,3,4,5,6,7,8,9,10,11,12,14] "12345678910" print(i) """ # 3. Please print: 12 3 4 5 8 9 10 """ for i in range(1,11): if i == 7: pass else: print(i) """
Note: For and while application scenarios: There is an infinite priority for, while infinite exhaustion
-
-
-
-
variable
-
Notes
-
Conditional statement
-
Loop statement: while + for + break + continue
-
operator
-
String formatting
- %s
- %d
- %%
-
Other
-
markdown Notes
-
git
-
Local: git software [Common commands]
- git status
- git add .
- Git commit-m''submit records'
- git push origin master
-
Remote: Code Cloud / github (Programmer Friendship Platform)
-
Interview related:
-
Write out the git commands you use frequently.
-
How did your company develop with git?
-
Create your own warehouse on code-hosted websites such as Code Cloud or GitHub. After you create it, Code Cloud will give me a warehouse address, such as: https://gitee.com/old_boy_python_stack_21/190326032.git
-
Write your own code.
-
Submit the code to the remote warehouse.
-
Initialization
-
Enter an arbitrary folder, such as: D: homework\
-
git init
-
git config mailbox
-
Name of git config
-
git remote add origin https://gitee.com/old_boy_python_stack_21/190326032.git
Note: Git has managed the D: homework directory at this point. Any changes in this folder will be detected by git (you can see the status using the GIT status command).
-
-
Code collection and submission
- git status
- git add .
- Git commit-m''record“
- The git push origin master synchronizes content in the local D: homework directory to the code cloud warehouse.
-
Modify the code or delete files, etc. to operate on any files under the local D: homework.
- git status
- git add .
- Git commit-m''record“
- The git push origin master synchronizes content in the local D: homework directory to the code cloud warehouse.
-
[Avoid] If there is remote code that is not available locally, it must be executed first: [May cause merge problems]
- git pull origin master
- git status
- git add .
- Git commit-m''record“
- The git push origin master synchronizes content in the local D: homework directory to the code cloud warehouse.
-
-
-
-
-
-
summary
- Syntax: Variable / if/while / Operator (Auxiliary)
- Necessary: Variables / if/while/
- Focus: Strings in data types
- Unique function
- Public function
- for
- To solve practical problems:
- Logic + Code
- Syntax: Variable / if/while / Operator (Auxiliary)
-
Detailed content
1. List
If you want to express two classmates users = Li Shao, Li Qihang... .
To represent multiple "things" later, you can use lists.
users = ["Li Shaoqi","Strange sailing",99]
-
Public function
-
len
users = ["Li Shaoqi","Strange sailing",99] val = len(users) print(val) # 3
-
Indexes
users = ["Li Shaoqi","Strange sailing",99] val = users[0] print(val)
-
Section
users = ["Li Shaoqi","Strange sailing",99] val = users[0:2]
-
Delete (except numbers/Booleans/strings)
users = ["Li Shaoqi","Strange sailing",99] # Mode I users.pop(1) print(users) # Mode 2: del users[1] print(users)
Be careful:
- String itself cannot be modified or deleted [immutable type] v1 = alex. upper()
- Lists are variable types.
-
Modification (except string/number/Boolean)
users = ["Li Shaoqi","Strange sailing",99] users[2] = 66 users[0] = 'Li Jie' users[0][1]
-
step
users = ["Li Shaoqi","Strange sailing",99] val = users[0:2:2]
-
Exercises
""" //Implement an integer addition calculator (adding two numbers): //For example: content = input("Please enter content:") User input: 5 + 9 or 5 + 9 or 5 + 9 (including blanks), and then partition conversion to the final integer calculation results. """ # Idea 1: """ content = input('Please enter:') # [5 + 9] or [5 + 9] or [5 + 9] content = content.strip() # [5+9] or [5+9] or [5+9] v1 = int(content[0]) v2 = int(content[-1]) v3 = v1 + v2 """ # Idea 2: """ content = input('Please enter:') # [5 + 9] or [5 + 9] or [5 + 9] content_len = len(content) index = 0 total = 0 while True: char = content[index] if char.isdigit(): total += int(char) index += 1 if index == content_len: break print(total) """ # Idea 3: """ content = input('Please enter:') # [5 + 9] or [5 + 9] or [5 + 9] result = content.split('+') # print(result) # ['55 ', ' 99 '] v1 = int(result[0]) # "55" v2 = int(result[1]) # " 99 " v3 = v1 + v2 print(v3) """
-
for loop
""" users = ['Li Shaoqi','Leech Airlines','Zhang Sanfeng','Li Zisen'] for i in users: print(i) """ """ users = ['Li Shaoqi','Leech Airlines','Zhang Sanfeng','Li Zisen'] for i in users: # First cycle: i= "Li Shaoqi" print(i) for ele in i: print(ele) """ # Exercise Question: Please implement through for loop and digital counter: users = ['Li Shaoqi','Li Qihang','Zhang Sanfeng','Li Zisen'] """ 0 Li Shaoqi 1 Leech Airlines 2 Zhang Sanfeng 3 Li Zisen """ """ # Mode I users = ['Li Shaoqi','Leech Airlines','Zhang Sanfeng','Li Zisen'] count = 0 for i in users: print(count,i) count += 1 """ """ # Mode 2 users = ['Li Shaoqi','Leech Airlines','Zhang Sanfeng','Li Zisen'] users_len = len(users) # 4 for index in range(0,users_len): # [0,1,2,3] print(index,users[index]) """
-
-
Unique function
-
Appnd, append an element to the end of the list
users = [] users.append('alex') print(users)
""" //Example 1: users = [] while True: name = input('Please enter your name.:') users.append(name) print(users) """ """ //Example 2: # Enter user and password users = [] for i in range(0,3): name = input('Please enter a username and password:') users.append(name) print(users) # ['alex,123', 'oldboy,888', 'lishaoqi,123'] # User and password verification username = input('Please enter the username to log in:') password = input('Please enter your login password:') for item in users: result = item.split(',') # ['alex','123'] user = result[0] pwd = result[1] if user == username and pwd == password: print('Successful landing') break """
-
insert
-
remove
-
pop
-
clear
-
-
Summary:
-
Add:
- append / insert
-
Delete:
- remove / pop / clear / del users[2]
-
Change:
- users[3]= "new value"
-
Check:
- Index/Slice
-
List nesting
users = ["alex",0,True,[11,22,33,"Old boy"],[1,['alex','oldboy'],2,3]] users[0] users[2] users[0][2] users[3] # [11,22,33,'Old Boy'] users[3][-1] # "Old Boy" users[3][-1][1] # 'male' users[3] = 666
-
2. Tuples
-
Tuple Writing Specification
users = [11,22,33,"Old boy"] # List (Variable) users = (11,22,33,"Old boy") # Tuples (immutable)
-
Public function
-
Index (excluding: int/bool)
users = (11,22,33,"Old boy") print(users[0]) print(users[-1])
-
Section (exclusion: int/bool)
users = (11,22,33,"Old boy") print(users[0:2])
-
Step size (excluding: int/bool)
users = (11,22,33,"Old boy") print(users[0:2:2])
-
Delete (exclude: tuple/str/int/bool)
-
Modification (exclusion: tuple/str/int/bool)
-
for loop (excluding: int/bool)
users = (11,22,33,"Old boy") for item in users: print(item)
-
len (exclusion: int/bool)
users = (11,22,33,"Old boy") print(len(users))
-
-
Unique function (no)
-
Special: Elements (sons) in tuples cannot be modified/deleted.
# Example 1: v1 = (11,22,33) v1[1] = 999 # error v1 = 999 # Correct # Example 2: You can nest v1 = (11,22,33,(44,55,66),(11,2,(99,88,),3)) # Example 3: Nesting v2 = [11,22,33,(11,22,33)] v2[-1][1] = 99 # error v2[-1] = 123 # Correct # Example 4: Nesting v3 = (11,[1,2,3],22,33) v3[1] = 666 # error v3[1][2] = 123
summary
-
What are the differences between interpretative and translational languages and how do you list the languages you know?
-
String Supplementary Function
- alone possess
- startswith/endswith
- format
- encode
- join
- public
- Section
- Indexes
- len
- Step size (interview questions)
- for loop
- range(0,10) # helps you generate a list of numbers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- Characteristic:
- It is immutable, so string elements cannot be deleted or modified.
- alone possess
-
git is synchronized locally and remotely, and can only be manipulated locally and submitted later.
-
List (Variable)
- public
- Indexes
- Section
- step
- modify
- Delete del
- for
- len
- alone possess
- append
- insert
- pop
- remove
- clear
- List nesting
- public
-
Tuples (immutable)
-
public
- Indexes
- Section
- step
- for
- len
-
Unique function (no)
-
Tuple nesting
v3 = (11,[1,2,3],22,33) v3[1] = 666 # error v3[1][2] = 123
-