python basic syntax

Hello everyone, this is still the longbow

Today we continue to learn python

In the previous article, we talked about the input and output of python

Today we first understand how python code works?

content

How the code executes - the compiler

type of data

Integers, floats and strings

boolean, null, variable, constant

Boolean value

Empty value: represented by None

variable

constant

python built-in data structures

list list

list basics

 list additions and deletions

tuple

 Conditional judgment and loop

if...else statement

How long have you been learning Mini Programs:

cycle

for...in loop

while loop

built-in structure

dict

set


How the code executes - the compiler

Students who have experience learning two languages ​​will find that the language of each language may be similar, but all are different.

For example output: You are awesome, classmates who are studying

//c语言
#include<stdio.h>
int main(){
  printf("你真棒,学习的同学");
  return 0;
}
#python
print("你真棒,学习的同学")

The two codes are different, how to achieve the same function?

This is because of the compiler in the language, it can tell the code to the computer and then execute the operation, but the bottom layer of the compiler in different languages ​​will be different. We don't need to go too deep in the beginner stage, we just need to remember that the compiler converts the code into something that the computer can understand. action performed .

                                       

It doesn't matter if you don't understand the first code here, we only need to pay attention to the process of executing different codes to produce results.

type of data

Do you remember the variables we used when we learned about typing? We only briefly explained variables at the time. As the name suggests, it is a variable that can be changed. Today, we will learn more about some common data types.

Python basic input and output - Longbow Dog's Blog - CSDN Blog

 Let's first understand three common data types

Integers, floats and strings

Integer is the same as mathematical concept, it is a value without decimals, including 0, positive and negative integers, such as 0, -1, 1.

For example, integers in c language are also subdivided into different integers, but python is not, so its value is not restricted.

Presumably everyone often hears binary, but it doesn’t matter if you haven’t. Let’s talk about the different bases of integers.

For details, please see the following article: Different bases of integers

-------------------------------------------------------------------------------------------------------------------------

Floating point numbers are what we know as decimals, such as 3.14, 1.4.

-------------------------------------------------------------------------------------------------------------------------

A string is the content in single quotation marks (') or double quotation marks ("), such as 'content', "content" is two strings. The output of our previous print("content") is a string.

When we want to output multiple lines of content in the string, the code is not very clear, such as

 Where \n is a newline character, so that the subsequent content is output on the next line, but the content of such conventional output code is not clear enough, Python provides a syntax

We use three quotation marks, enter the first line and press Enter to get the following picture

 Then input three lines of data, and get multi-line output of the data

                                        

Next, let's look at four words that are not so frequently used.

boolean, null, variable, constant

Boolean value

We can think of booleans as an either-or straight man

                                         

Booleans have only two values, True and False.

 So what is it good for? We will use the conditional judgment we will talk about later, and we will not talk about it here.

Empty value: represented by None

variable

For example, the name we used before can be used to store other data types, but we need to give it a variable name, and name is the variable name.

What is a variable name? The variable name must be _a combination of uppercase and lowercase English, numbers and numbers, and cannot start with numbers

constant

Constants are numbers that do not change, usually expressed in all uppercase letters, such as

NUM = 20220320

python built-in data structures

list list

list basics

A list is a variable-sized ordered collection that can be used to store multiple contents,

 Python also provides a len function to display the number of elements in the list

We can also access elements in the list in the following ways

Note that you need to start from 0 to correspond to the first element

 We can also display from back to front

Be careful not to cross the line, there will be unspecified situations

                             

 list additions and deletions

For example, we add an element to the end of list1

 What if we want to add the element to the specified position?

Or how to delete the tail and specify the position to delete?

 The last is to replace, directly assign

tuple

Tuples are like lists, except that the elements in a tuple are immutable . Therefore, it is necessary to give the data when initializing.

 Conditional judgment and loop

We hope that the computer can output different results in different situations, here we will use

if...else statement

How long have you been learning Mini Programs:

We use three months to determine a person's identity, and stipulate that a person under three months is a beginner, and he can be praised for more than three months:

We first encountered such an error, why?

 Because the input data type is str string type, it cannot be directly compared with the integer 90

But fortunately, python provides the int() function to convert data types

According to this, we change the code, the source code is as follows:

print("你学多久了啊?")
print("请输入天数:")
DayNumber = input()
DayNumber = int(DayNumber)
if DayNumber<90:
    print("才",DayNumber,"天,初学者,仍需努力哦!")
else:
    print(DayNumber,"天这么久了,可以啊")

 

cycle

 There are two kinds of loops in python, why do you need loops? For example, we calculate the accumulation of 1 to 100, in order to simplify the process of manual input.

for...in loop

add = [1,2,3,4,5]
for x in add:
    print(x)

 For example, we implement a 1-5 accumulation.

sum = 0
add = [1,2,3,4,5]
for adds in add:
    sum = sum + adds
print(sum)

 What if we want to achieve the accumulation of the first 100?

Python provides the range() function, which is implemented with the help of this function

 code show as below:

sum = 0
for adds in list(range(101)):
    sum = sum + adds
print(sum)

while loop

When the statement after the while is true, continue to execute, when the statement is not satisfied, end the loop

Or to achieve the addition of the first 100

sum = 0
n=0
while n<101:
    sum = sum + n
    n = n + 1
print(sum)

                                                    

built-in structure

dict

dict is called a dictionary, how is it used?

For example, we write a table to record the birthdays of students

di = {'张三':11,'李四':22,'王五':33}
di['李四']

 We see that the data above are all in a one-to-one correspondence. We can query the value 2 through the front word B. This structure is called a key-value pair. The key key corresponds to the value value, B is the key, and 2 is the value.

When the key does not exist, an error will be reported as above. 

                                           

So are we avoiding the wrong situation?

Naturally there is

We can use in, when it is False, that is, there is no such key value

You can also use the get() function in dict, as shown in the figure below for two methods of use

 Finally, the elements in the dict are deleted, and pop() can also be used

set

Set and dict are very similar, the difference is that only the key value is stored and cannot be repeated.

We can see that in set(), a []list is needed to initialize the data.

So how does it add or delete data?

Python provides add() and remove() interfaces

It is also concluded here that add() can only insert a single piece of data at a time.

                                                 

Today's content is here

Here's the longbow

Thumbs up for the students who saw this

Next time we will explain the use of functions in programming

see you next time...

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324319205&siteId=291194637