@[TOC]
preface
I don’t know if you will be impressed by the simplicity of Python, but I feel comfortable writing Python code.
So now that you’ve learned about basic data types and branch control statements, what about advanced data types
Practice code
- Mathematical flow chart code
Topic:
Code:
x = int(input()) # Receive keyboard input values
while True: # Define a loop that keeps the judgment going until the condition is met and it stops
y = x/2 - 1 # Assign a value to y
if abs(y-x)<1: # Determine the difference between absolute values
print(y) Print y value if condition is met
break # Break the cycle
else:
x = y # if not, assign the value of y to x
Copy the code
- Topic: 99 Multiplication table
Code:
for x in range(9) :# 99 Multiplication table, 9 columns, loop 9 times
i = x+1 # Because x starts at 0, so let's make x plus 1
# Each row has data and is different, so nesting is also needed
for y in range(i): # Each row requires I results
j = y+1
print(f"{i}*{j}={i*j}", end="") Print each line of data, followed by end=" "to achieve non-newline output
print(a)# Line feed at the end of each line
Copy the code
- Topic: Input a number, determine whether the number of daffodils
1**3+5**3+3**3=153
num = input(a)# Receive a string
sum = 0
# said and
for i in num:
x = int(i)
y = x**3
sum+=y
# Take the input numbers apart to the third power and sum them up
if sum= =int(num):
print(num, "It's the number of daffodils!)
else:
print(num, "Not the number of daffodils!)
Copy the code
If you can’t do it, it doesn’t mean anything. You need more practice, right
Advanced data type
What is advanced data? Why is this type called advanced? I’ve talked about it all before — basic grammar
But in order to make up the number of words did not see a small partner, or say it
Advanced data types, compared with basic data types, because they have more complex operations, and the most worth saying, and should be worth to master is the operation of adding, deleting, changing and checking. Of course, advanced data types can store more data than basic data types, which can store only one data at a time.
Add, delete, modify and search: Add, delete, modify and search (view)
Advanced data types include: string (I did not expect to learn programming is the first contact with advanced data types), list, set, tuple, dictionary, class (this is not to understand, will be described in detail later, is difficult)
1. The string
Strings are just like what we would say, strings together, called strings, and the characters are one by one
1.1 traversal
Well, first of all, the string can iterate over values, which means we can use our loop to show all the elements in it, one by one, take them out, and do all kinds of things
Of course, other advanced data types can also be traversed, but the dictionary will be different
The code shown
str_ = "Hello World!"
for i in str_:
print(i)
Copy the code
Running result:
As you can see, the loop takes our strings apart one by one and outputs them
1.2 Formatting Strings
What is a formatted string? It just makes our code a little bit simpler
Understanding by problem
How to output a sentence that contains variables
Example: given a variable a = 1, how can the output result be: “the value of a is: 1”
- Print (“a has a value of: “, a)
- Print (“a = {}”.format(a))
- Print (” value of a: %d”%a)
- Print (f”a “= {a}”)
The first method is cumbersome when dealing with multiple variables in a string. In this case, we need to use the following three methods. The simplest method is method 4. Because it’s easy to write
1.3 Native Strings
A native string is just a string. Unlike formatting, everything in the quotation marks of a native string is a string and is not recognized by the machine as anything else
For example, our escape character \
\t means our TAB \n means newline
So how do we define that? You just put an R in front of the parentheses before the string
So if we look at this code, what does it look like
print(R "Hello \t world")
print(R "Hello \n world")
Copy the code
Running result:
You can see that our escape character doesn’t do anything, it’s actually represented, and that’s what our native string does, right
About escape character, I don’t say much, like know can go to search
As for strings, there are many more operations, such as determining whether a string is a number, determining case, turning case, and so on. If you want to know all the operations that can be used to search for strings, this article will not say that much as an introduction.
List 2.
List the list
A list is a set of data enclosed in brackets such as this one [1, 2, 3, 4, 5,] is a list
This is just a list of five integers from 1 to 5
Accept a list as a variable: li = []
As an advanced data type, a list can be added, deleted, and reviewed.
As far as traversal goes, it’s the same thing as a string
You don’t need to understand, you just need to know that lists are bags that can help us store more stuff.
And we can also use this bag to operate the inside of the things, such as the above add, delete, change and check, is not to put things in the bag, take out the bag of things, take out the bag for something, see the bag of things
2.1 Putting things in bags
To add an element to the list: li.append()
For example:
li = [1.2.3.4]
li.append(5)
print(li)
Copy the code
Results: [1, 2, 3, 4, 5]
As an advanced data type, you can add any data type to the list
2.2 Take out the contents of the bag
To remove a specified element from a list: li.remove()
For example:
li = [1.2.3.4]
li.remove(1)
print(li)
Copy the code
Results: [2, 3, 4]
You take the 1 out of the bag, and where you put it, you throw it away.
2.3 Take the contents out of the bag and change it for something else
Find elements:
Lists support finding elements, but using subscripts, which start at 0 in Python, and of course, strings also support subscripts
For example:
li = [1.2.3.4]
print(li[0])
Copy the code
The result is: 1
Since we are talking about the following table values, let’s talk about slices and step sizes
Slice: You can limit the range of subscripts you take
Step size: take several elements
List [subscript: the following table: step size] 1 indicates that positive values are taken one by one. -1 indicates that positive values are taken backwards. If you do not write, the default value 1, 2 indicates that positive values are taken once every other time
Let’s say we only take the elements of 1,3
a = [1.2.3.4.5.6]
print(a[1:3])
# result: [2, 3]
print(a[::2])
# Result: [1, 3, 5]
print(a[;;-1])
# Result: [6, 5, 4, 3, 2, 1]
Copy the code
Look at the contents of the bag
Modify element:
Again, elements need to be modified by subscript
For example:
li = [1.2.3.4]
li[0] = 2
print(li)
Copy the code
The results are: [2, 2, 3, 4]
Set 3.
Set: Sets have disorder, and unrepeatable disorder: there is no order
You can also create an empty set, but you can’t create s = {}
Because this is defining an empty dictionary, we should set(s) like this.
Format (what it looks like) :
s = {1, 2, 3, 4, 5}
3.1 Adding Elements
Add element s.dd ()ands. Update () to the collection
s = {1.2.3.4.5}
s.add(6)
s.upadte(7)
print(s)
Copy the code
The result is: {1, 2, 3, 4, 5, 6, 7}
3.2 Deleting Elements
s = {1.2.3.4.5}
s.remove(1)
print(s)
Copy the code
The result is: {2, 3, 4, 5, 6}
Delete all: s.clear()
3.3 Modifying Elements
There is no such function because the elements are unordered and cannot be found or modified by subscript
That’s fine, because you use lists and dictionaries a lot in your program
4. A tuple
Tuples, unlike other tuples, are immutable, meaning they cannot be added, modified, or deleted
Tuples use parentheses to define tu = (1, 2, 3, 4)
Like a list, a collection, or a dictionary, each group of elements is separated by a comma
You can refer to lists, but tuples can’t be modified like collections, but not because of disorder, tuples can be evaluated by subscript
4.1 Viewing tuples
We can print tuples directly, use subscripts, slice and step sizes
tu = (1.2.3.4.5)
print(tu)
print(tu[0:4:1])
Copy the code
Results :(1, 2, 3, 4, 5)
4.2 delete
Although tuples are immutable, we can simply delete the entire tuple
tu = (1.2.3.4.5)
del tu
print(tu)
Copy the code
The result is :()
5. The dictionary
Why return the dictionary at the end? Because dictionaries are in the form of key-value pairs.
Format: dic = {“key”: value}
Keys must be unique, but values do not.
The value can take any data type, but the key must be immutable, such as a string, a number.
In a dictionary, access the key to get the value
5.1 Adding Elements
We can define an empty dictionary or we can define a dictionary with elements
We’ll give examples of each
An empty dictionary:
di = {}
di["name"] = "Tom"
di["age"] = 18
print(di)
Copy the code
Result: {“name”: “Tom”, “age”: 18}
A dictionary with elements:
di = {"name": "Tom"}
di["age"] = 18
print(di)
Copy the code
{“name”: “Tom”, “age”: 18}
And the reason we can do this is because our key is unique, so we can add elements this way
5.2 Deleting Elements
Or is it determined by a unique key and then deleted
di = {"name": "Tom"."age": 18}
del di["age"]
print(di)
Copy the code
{“name”: “Tom”}
5.3 Modifying Elements
Reassign by key to modify elements, which I won’t explain or demonstrate
5.4 Searching for Elements
You can’t find an element by subscript, you need to find it by key
di = {"name": "Tom"."age": 18}
print(di["name"])
Copy the code
The result: “Tom”
conclusion
Interest is the best teacher, adhere to the truth is the same. Learn not to be impatient, step by step, steadfast forward. Make a little progress every day, and over time, you will find that you have become a great person.
I am bu small Zen, a self-study MOE new, follow me every day progress a little bit!
That’s enough for the time being, and I’ll leave you