The first day of Python study

Time:2021-4-6

Python

Why so many training institutions? So many programmers?

It’s very simple. becauseThe threshold is low。 ==.

Don’t believe it? Let’s try.

1. Operating system

target

  • Understand the operating system and Application

Operating system interface diagram

The first day of Python study

For computers that don’t have an operating system installed, that’sbare pager

2. Advantages and disadvantages of Python

  • Time makes heroes, life is short, I use python

The first day of Python study

What can Python do?

The first day of Python study

3. The first program in Python

I’ve said something in the last part, but someone asked meHow to create!

Here withjupyterFor example.

If it is fried chicken Xiaobai.

1. Run the service and enter the web page

The first day of Python study

2. Go to your work directory

The first day of Python study

3. Successfully output your first line of code

The first day of Python study

4. Notes

4.1 introduction of notes

1. Note not used

The first day of Python study

2. Use notes

The first day of Python study

3. The function of annotation

Through the use of their own familiar language, in the program for some code annotation, this is the role of annotation, can greatly enhance the readability of the program

4.2 annotation classification

1. < single line notes >

#I'm a comment. I can write some function descriptions in it
    print('hello world')

2. < multiline comment >

The first day of Python study

3. < note >

The recommended method in Python syntax specification is as follows:

(early version, if not written,`PrintYou will report an error.)

# -*- coding:utf-8 -*-

5. Variables and types

target

  • Master the definition and modification of variables
  • Understand the types of variables and how to view them

5.1

A: In short, in the program, we need to sum two data, so what should we do?

B: Analogy in real life, such as you go to buy breakfast, an egg, an eggplant bun, etc., you need to calculate how much you spend.

So, for Python, to store a data, you need avariable

The first day of Python study

  • Program is used to process data, and variable is used to store data.

*Thinking: how much space should we let variables take up and what kind of data should we store? *

5.2

Examples of types in life (take cars for example)

Excavators, cars, sports cars, buses

(different types, sizes and prices)

In the procedure:

Make full use of space and manage memory effectively

The first day of Python study

So the question is, how do you know the type of a variable?

  • In Python, as long as a variable is defined and it has data, its type is determined. It is not necessary to specify its type, the system will automatically identify it.
  • Type (variable name) — View variable type

The first day of Python study

6. Identifiers and keywords

6.1 identifier

Some symbols and names defined by developers in the program

Identifier is defined by itself, such as variable name, function name, etc

6.2 rules of identifier

The identifier consists of letters, underscores and numbers, and the number cannot begin

Find out: (as follows, which are right and which are wrong?)

fromNo12
from#12
my_Boolean
my-Boolean
Obj2
2ndObj
myInt
test1
Mike2jack
My_tExt
_test
test!32
haha(da)tt
int
jack_rose
jack&rose
GUI
G.U.I
a2data

Identifiers in Python are case sensitive

The first day of Python study

6.3 naming rules

  • See the name and know the meaning
  • Hump naming
  • Underline links

The first day of Python study

6.4 key words

Python has some special features of the identifier, which is the so-called keyword

Keyword is already used by python, so developers are not allowed to define the identifier of the same name as the keyword

View keywords

The first day of Python study

7. Output

7.1 general output

#Print tips
print("hello world")

7.1 format output

7.1.1 purpose of formatting

Xiaofeng is 10 years old
Xiaofeng is 18 years old
Xiaofeng is 20 years old

#Thinking, age used many times, how to simplify -- string visualization

7.1.2 format

The first day of Python study

7.1.3 line feed output

#When outputting, if there is any, then the contents after will be displayed in another line

Print ("1234567890 -------") will be displayed on one line

Print ("1234567890 / N -------")? One line displays 1234567890 and the other line displays 1234567890-------

7.1.4, python 2-3 output difference

Using print in Python 3, you must wrap the printed content in parentheses, such asprint('hi')

Python 2 can use either parentheses or a space to separate the printed content, such asprint 'hi'

Now it’s mainly based on Python 3

7.1.5 try it

Code to display the business card:

==================================
Name: Xiaofeng who makes money hard    
QQ:xxxxxxx
Mobile number: 185xxxxx
Company address: Beijing
==================================

#How to format the output? A new line?

8. Input

Let’s briefly say that the input in Python 2 is raw_ Input() function

In Python 3, only input ()

The first day of Python study

9. Operator

  • Arithmetic operator

    • Operation priority
  • Assignment Operators
  • compound assignment operators

9.1 arithmetic operators

Take a = 10, B = 20 as an example to calculate

operator describe example
+ plus Two objects add a + B and output 30 results
reduce Get a negative number or a number minus another number a – B, output – 10
* ride Multiplication of two numbers or return a string a * B repeated several times, output result 200
/ except X divided by Y B / a outputs result 2
% Surplus Returns the remainder of the division, B% a, and outputs 0
** power Returns the Y power of X, a * * B is the 20th power of 10, and the output is 1000000000000000
// Division by integer Returns the integer part of the quotient, 9 / / 2, output 4, 9.0 / / 2.0, output 4.0
>>> 9 / 2.0
4.5
>>> 9 // 2.0
4.0
>>> 1 + 2*3
7
>>> (1+2) * 3
9

9.2 assignment operator

operator describe example
= Assignment Operators Give the result on the right side of the = sign to the variable num = 1 + 2 * 3 on the left. The value of num is 7
>>> a, b = 1, 2
>>> a
1
>>> b
2

9.3. Compound assignment operator

operator describe example
+= Additive assignment operator C + = a is equivalent to C = C + a
-= Subtraction assignment operator C – = a is equivalent to C = C – A
*= Multiplication assignment operator c =A is equivalent to C = C a
/= Division assignment operator C / = a is equivalent to C = C / A
%= Modulo assignment operator C% = a is equivalent to C = C% a
**= Power assignment operator c =A is equivalent to C = C a
//= Integer division assignment operator C / / = a is equivalent to C = C / / A

10. Data type conversion

  • Understand the role of type conversion
  • Master common type conversion
function explain
int(x [,base ]) Convert x to an integer
long(x [,base ]) Convert x to a long integer
float(x ) Convert x to a floating point number
complex(real [,imag ]) Create a plural
str(x ) Converts object x to a string
repr(x ) Converts object x to an expression string
eval(str ) Used to evaluate a valid Python expression in a string and return an object
tuple(s ) Convert sequence s to a tuple
list(s ) Convert sequence s to a list
chr(x ) Converts an integer to a character
unichr(x ) Converts an integer to a Unicode character
ord(x ) Converts a character to its integer value
hex(x ) Converts an integer to a hexadecimal string
oct(x ) Converts an integer to an octal string

give an example

A ='100 '# at this time, the type of a is a string in which the three characters of 100 are stored
B = int (a) # in this case, the type of B is integer and the number 100 is stored in it

print("a=%d"%b)

11. Introduction to judgment sentences

Let’s just take an example.

Important date judgment cases

If today is Saturday or Sunday:
        Date sister

    If today is Valentine's Day:
        Buy roses

    If you pay today:

        Pay back the credit card first

        If there is surplus:

            Happy again, O (∩)_ (laughter)~

        else:

            Oh, No... I'll have to wait 30 days

Summary:

  • Only when certain conditions are met can we do something, but not when they are not met. This is called judgment
  • Not only in life, but also in software development, the “judgment” function is often used

12. If statement

  • Master the syntax format of if statement

12.1 introduction of if judgment statement

  • The if statement is used for judgment, and its format is as follows:
If conditions to judge:
        What to do when conditions are established
  • demo1:
age = 30

    Print "---- if judge start ---"

    if age>=18:
        Print "I'm an adult"

    Print "---- if judge end --"
------If judgment starts------
    I'm an adult
    ------If judge end------
  • demo2:
age = 16

    Print "---- if judge start ---"

    if age>=18:
        Print "I'm an adult"

    Print "---- if judge end --"
  • Results of operation:
------If judgment starts------
    ------If judge end------

Summary:

  • The above two demos only show that the value of the age variable is different, but the result is different. We can see that the function of the if judgment statement is to execute the code only when certain conditions are met, otherwise the code will not be executed

be careful:

  • The code is indented with a tab or four spaces

12.2 practice

Requirements: get your age from the keyboard, judge whether it is greater than or equal to 18 years old, and output “brother, adult, Internet cafe can go” if it meets the requirements

  1. Use input to get data from the keyboard and store it in a variable
  2. Use the if statement to determine whether age > = 18 is true

12.3 think about it

  • If age is greater than or equal to 18 years old, we use > =. What else?

13. Comparison, relational operators

13.1 comparison operator

The comparison operators in Python are shown in the following table

operator describe Examples
== Checks whether the values of the two operands are equal, and if so, the condition becomes true. If a = 3, B = 3, then (a = = b) is true
!= Checks whether the values of two operands are equal. If the values are not equal, the condition becomes true. If a = 1, B = 3, then (a! = b) is true
<> Checks whether the values of two operands are equal. If the values are not equal, the condition becomes true. If a = 1, B = 3, then (a < > b) is true. This is similar to the! = operator
> Check whether the value of the left operands is greater than that of the right operands. If so, the condition holds. If a = 7, B = 3, then (a > b) is true
< Check whether the value of the left operands is less than that of the right operands. If so, the condition holds. If a = 7, B = 3, then (a < b) is false
>= Check whether the value of the left operands is greater than or equal to the value of the right operands. If so, the condition holds. If a = 3, B = 3, then (a > = b) is true
<= Check whether the value of the left operands is less than or equal to the value of the right operands. If so, the condition holds. If a = 3, B = 3, then (a < = b) is true

13.2 logical operators

operator Logical expression describe example
and x and y Boolean “and” – if x is false, X and Y return false, otherwise it returns the computed value of Y. (A and b) return to 20.
or x or y Boolean or – if x is true, it returns true, otherwise it returns the computed value of Y. (a or b) returns 10.
not not x Boolean not – Returns FALSE if x is true. If x is false, it returns true. Not (A and b) returns false

14. Xiaofeng homework practice

Make a question

  1. Say the name of the variable. What characters can it consist of
  2. Write the rules for variable naming
  3. What is hump method (big hump, small hump)
  4. Write the program and complete the following requirements:

    • Prompt the user to input data
    • Get user data (2 required)
    • Sum the two numbers and output the corresponding results
  5. Write the program and complete the following requirements:

    • Prompt the user to input data
    • Get user data (2 required)
    • Subtract the two numbers and output the corresponding results
  6. Write a program to display the following information:
==================================
=Welcome to Authentication System v1.0
=1. Login
=2. Exit
=3. Certification
=4. Change the password
==================================
  1. Write a program to get a person’s information from the keyboard, and then display it in the following format
==================================
Name: a2data  
QQ:xxxxxxx
Mobile number: 185xxxxx
Company address: Beijing
==================================
  1. Write a program, get the user name and password from the keyboard, and then judge, if correct, output the following information
Dear XXX, welcome to a2data learning management system

Author blog a2data!