Python entry learning record (1)

Lesson 1: A happy start

Summary of knowledge points
1. Application scope of Python: operating system, 3D animation, WEB, enterprise application, cloud computing, etc.
2. What type of language is Python? Scripting languages, i.e. computer programming languages, are simpler and easier than systems programming languages ​​like C, C++ or Java.
3. What is a scripting language? A script can automate interactive operations that would otherwise be performed using the keyboard. A shell script mainly consists of commands that would otherwise need to be entered on the command line, or in a text editor, the user can use the script to combine some commonly used operations into a set of sequences. The language primarily used to write such scripts is called a scripting language. Many scripting languages ​​actually go beyond the simple serialization of user commands to write more complex programs.
4. What features does a scripting language have? The syntax and structure are simple, the learning and use are simple, and the "interpretation" of the program that is easy to modify is usually used as the running method, without the need for "compiling", and the development productivity is better than the running performance.

Lesson 2: My first close encounter with python

Summary of knowledge points
1. What is IDLE? It is a Python Shell, basically, a way to interact with the program by typing text, similar to the cmd window in windows.
2. The output window displays text: print()
3. Simple operations:

>>>5+8
13
>>>5*8
 40
>>> print('str '*5)
str str str str str
>>> print('str1'+'str2')
стр1стр2
>>> print('str '+5)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    print('str '+5)
TypeError: must be str, not int
Reason for the error: The plus sign in python represents addition between numbers and splicing between strings, but does not allow the addition of two different forms (such as numbers + strings).

Lesson 3: Designing Your First Game in Python

Summary of Knowledge Points
1. What is BIF? BIF (Built-in Functions) built-in functions, a total of 68, can be called directly, which is convenient for programmers to quickly write script programs. Enter dir(__builtins__)to view all built-in functions, and you help(function_name)can view the function description of the function.
2. Python is case sensitive.
3. Pay attention to code indentation when writing python code. If you enter a colon ":" in a normal position, IDLE will automatically indent the next line.
4. Python does not allow assignment (and no parentheses) in the if condition, that is, the judgment condition should be written as if i==1:, the assignment number "=" and the judgment equality sign "==" should be strictly distinguished.
5. Variables in python do not need to be declared in advance, but must be assigned a value before use.
6. 

temp=input( "To play the guessing game, please input: " )
 guess = int(temp)
  if guess==8 :
      print ( " guessed correctly " )
  else :
      print ( " guessed wrong, no chance " )
  print ( " game over " )

input() function: reads a string from standard input. If a prompt string is given, the content in parentheses is output first, without a newline before reading the input string.
Why guess=int(temp)? Because temp is a string type (reference type), and == is a comparison for basic data types, including int, double, etc.

Lesson 4: Vignette Variables and Strings

Summary of knowledge points
1. There are no variables in python, only pointers, strictly speaking, pointer variables. (You can understand, the following knowledge points are still described by "variables")

>>>a=5
>>>b=5
>>>id(a)
1498728240
>>>id(b)
1498728240
>>>id(5)
1498728240

It a=5is not an ordinary assignment statement, but 5a name is given a, that is, the pointer apoints to 5, so their memory addresses are the same.
2. Variable name specification:
assign value before use.
Variable names can include letters, numbers, and underscores, but cannot start with numbers.
Case sensitive.
The name should be meaningful.
3. String: The escape symbol escapes the quotation marks in the string

>>>'let\'t go'

"let't go"

>>>"let't go"

"let't go"

>>>print('let\'t go')

let't go

The escape character escapes backslashes in a string.

>>> print("C:\now")
C:
ow
>>>print("C:\\now")
C:\now

 The use of raw strings, add r, (automatically add escape characters to each backslash, saving manual trouble), another note, the end of the raw string cannot be a backslash, otherwise it will be followed by a single quote or double quote Together they are treated as escape characters and an error occurs. The last method can be used to solve.

>>> print(r"C:\now")
C:\now
>>> r"C:\now\hour\min"
'C:\\now\\hour\\min'
>>> print(r"C:\now\hour\min")
C:\now\hour\min
>>> print(r"C:\now\hour\min\")

SyntaxError: EOL while scanning string literal
>>> print(r'C:\now\hour\min''\\')
C:\now\hour\min\

Long strings: Use paired single or double quotes.

>>> str= ''' first line,
second line,
The third row,
the last line. ''' 
>>> str
 ' The first line,\nthe second line,\nthe third line,\nthe last line. ' 
>>> print (str)
first row,
second line,
The third row,
the last line.

Lesson 5: Improving our little game

Summary of knowledge points
1. Improve game requirements:
- The program should give some hints when guessing wrong, such as whether the guess is too big or the guess is too small.
- Each time the program is run, there are multiple chances to guess the number (fixed number).
- Every time you run the program, the answer should be random.

#The code is changed by myself, if there is any error, please correct 
import random

secret =random.randint(1,10 )
 print ( " --------------Number Guessing Game------------ " )
num =5
 print ( " You have %d game chances! " % num)
 while num> 0:
    temp =input( " Please enter a number: " )
    guess = int(temp)
     if guess== secret:
         print ( " you guessed it right " )
         print ( " you won, the game is over " )
         break 
    else :
         if guess> secret:
              print ( " guessed the big one " )
         else :
             print ( " Guess is too small " )
    num =num-1
     if num== 0:
         print ( " Fail, you don't have a chance to play " )
         break 
    else :
         print ( " You have %d chances left! " %num)

andOperation Logic: Concatenate arbitrary expressions together and get a boolean value.
randomFunction in module randint: Returns a randomly generated integer.
python does not support do-while statement, you can use while and if in combination

Guess you like

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