Python self - Basic knowledge Summary (1)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wulishinian/article/details/102725660

The first question, What is Python? According to the father of Python Guido van Rossum, then, Python is:

A high-level programming language, its core design philosophy is code readability and syntax that enables programmers with very little code to express their own ideas.
For me, the primary reason is to learn Python, Python is a programming language can be elegant. It can be simple and natural to write code and implement my ideas.

Another reason is that we can use Python in many places: scientific data, Web developers and machine learning and so can use Python to develop. Quora, Pinterest and Spotify use Python to their back-end Web development. So let's learn about Python it.

Python basis

  1. Variable
    you can think of a variable to store the value of a word. We look at an example.
    Python is variable and it is very easy assignment. If you want to store a number from 1 to variable "one", let's give it a try:
   one = 1

Super simple, right? You just need a value of 1 is assigned to the variable "one".

  two = 2
  some_number = 10000

As long as you want, you can put an arbitrary value assigned to any other variable. As you can see from the above, the variable "two" store integer variable 2, variable "some_number" storage 10000.
In addition to integer, we can use Boolean values (True / Flase), string, float, and other data types.

# booleanstrue_boolean = Truefalse_boolean = False# stringmy_name = "Leandro Tk"# floatbook_price = 15.80
  1. Control flow: conditional statement
    "If" using an expression to determine if a statement is True or False, True if it is, then if the code in the execution, the following examples:
if True:
print("Hello Python If")
if 2 > 1:
print("2 is greater than 1")

2 greater than 1, the print code is executed.
When the "if" inside expression is false, "else" statement is executed.

if 1 > 2:
print("1 is greater than 2")
else:
print("1 is not greater than 2")

Less than 12, so the "else" code inside will execute.
You can also use the "elif" statement:

if 1 > 2:
print("1 is greater than 2")elif 2 > 1:
print("1 is not greater than 2")else:
print("1 is equal to 2")
  1. And the iteration loop
    in Python, we can iterate different forms. I would say the next while and for.
    While the cycle: when the statement is True, while the interior of the block is executed. Therefore, the following code will print out 1-10.
num = 1
while num <= 10:
    print(num)
    num += 1

The while loop cycles required conditions, if the condition has been True, it will always iteration, when 11 value of num, loop condition is false.
Another piece of code can help you better understand usage while statement:

loop_condition = Truewhile loop_condition:
    print("Loop Condition keeps: %s" %(loop_condition))
    loop_condition = False

Cycling conditions were True it would have been iterated until to False.
For cycling: you can use variable "num" on the block, and "for" statement will iterate over you. This code will print while the same code: from 1 to 10.

for i in range(1, 11):
    print(i)

I did not see? It's too simple. I ranging from 1 up to the start of the element 11 (element 10 is a tenth)

List: collection | arrays | data structure
if you want to store in a variable integer 1, but you have to store 2 and 3, 4, 5 ...
not with hundreds of variables, I have other methods to store these I want integer stored there? You've guessed it, they do have other storage methods.
List is a collection that can store a value (such as if you want to store), so let's take a look at it:

my_integers = [1, 2, 3, 4, 5]

It's really quite simple. We create an array called the my_integer and the stored data to the inside.
You might ask: "How do I get the array of values?"
Good question. A list of the concept called index. The first element in the table is the index 0 (0). The second index is 1, and so on, you should understand.
To make it more simple, we can use the index represents an array element. I drew it out:
Here Insert Picture Description
using Python syntax, but also good to understand:

my_integers = [5, 7, 1, 3, 4]
print(my_integers[0]) # 5print(my_integers[1]) # 7print(my_integers[4]) # 4

If you do not want to store integer. You just want to keep some strings, like a list of the names of your relatives. My looks like this:

relatives_names = [  "Toshiaki",  "Juliana",  "Yuji",  "Bruno",  "Kaio"]
print(relatives_names[4]) # Kaio

It's the same principle with the deposit integer, very friendly.
We only learned how to index list is working, I need to tell you how to add an element to the data structure of the list (add an item to the list).
The most common way to add new data to the list is spliced. Let's look at how it is used:

bookshelf = []
bookshelf.append("The Effective Engineer")
bookshelf.append("The 4 Hour Work Week")
print(bookshelf[0]) # The Effective Engineerprint(bookshelf[1]) # The 4 Hour Work W

Stitching super simple, you just take one element (such as "efficient machine") as the stitching parameters.
Well, knowledge about the list of these is enough, let's look at other data structures.

Dictionary: Key-Value data structure

Now we know that List is indexed set of integer numbers. But if we are not using integer numbers as the index it? We can use some other data structures, such as numbers, strings, or other types of indexes.
Let us learn this next dictionary data structure. A dictionary is a collection of key-value pairs. Dictionary almost a long way:

dictionary_example = {
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

Key points to the index value. How do we access the dictionary value it? You should have guessed, it is to use key. Let's try this:

dictionary_tk = {
  "name": "Leandro",
  "nickname": "Tk",
  "nationality": "Brazilian"
}
print("My name is %s" %(dictionary_tk["name"])) # My name is Leandro
print("But you can call me %s" %(dictionary_tk["nickname"])) # But you can call me Tk
print("And by the way I'm %s" %(dictionary_tk["nationality"])) # And by the way I'm Brazilian

We have a key (age) value (24) , as a key string as an integer value.
I created a dictionary about me, including my full name, nickname and nationality. These properties are in the dictionary key.
Just as we have learned to use the index to access list, we use the same index (key index is in a dictionary) to access the value stored in the dictionary.
As we use the list as we learn how to add elements to the next dictionary. The main point is the dictionary value of the key. When we add an element of the same is true:

dictionary_tk = {
  "name": "Leandro",
  "nickname": "Tk",
  "nationality": "Brazilian",
  "age": 24
}
print("My name is %s" %(dictionary_tk["name"])) # My name is Leandro
print("But you can call me %s" %(dictionary_tk["nickname"])) # But you can call me Tk
print("And by the way I'm %i and %s" %(dictionary_tk["age"], dictionary_tk["nationality"])) # And by the way I'm Brazilian

A key we only need to point to a dictionary of a value. It is not that hard, right?

Iteration: circulates through a data structure

Like we learned in Python foundation, List of iterations is very simple. We Python developers often use a For loop. We give it a try:

bookshelf = [
  "The Effective Engineer",
  "The 4 hours work week",
  "Zero to One",
  "Lean Startup",
  "Hooked"
]
for book in bookshelf:
    print(book)

For every book on the shelf, we print (do anything) to the console. Super simple and intuitive it. That's the beauty of Python.
For hashed data structure, we can also use a for loop, but we need to use the key.

dictionary = { "some_key": "some_value" }
for key in dictionary:
    print("%s --> %s" %(key, dictionary[key])) # some_key --> some_value

The above is an example of how to use the For loop in the dictionary. For each dictionary key, we print out the key and the key corresponding to the value.
Another method is to use iteritems.

dictionary = { "some_key": "some_value" }
for key, value in dictionary.items():
    print("%s --> %s" %(key, value))# some_key --> some_value

We named two key parameters and value, but this is not necessary. We can name. We look:

dictionary_tk = {
  "name": "Leandro",
  "nickname": "Tk",
  "nationality": "Brazilian",
  "age": 24
}
for attribute, value in dictionary_tk.items():
    print("My %s is %s" %(attribute, value))
# My name is Leandro
# My nickname is Tk
# My nationality is Brazilian
# My age is 24

We can see that we use the attribute as a key parameter in the dictionary, which is named using key has the same effect.

Guess you like

Origin blog.csdn.net/wulishinian/article/details/102725660