Article 1: python basics 1, Article 1 python_1

Source: Internet
Author: User

Article 1: python basics 1, Article 1 python_1

 

Content

 

1. Introduction to Python

The founder of Python is Guido van rosum. During the Christmas Day of 1989, in Amsterdam, Guido was determined to develop a new script interpreter as an inheritance of the ABC language in order to make Christmas boring. The reason why Python is selected as the programming language name is because it is a fan of a comedy group called Monty Python.

Ranking of Tianyi programming languages in June 2017:

As you can see, the usage of python is on the rise, while that of the top three is on the decline.

 

Python Advantages and disadvantages

Advantages:

Disadvantages:

 

Ii. Install windows in Python
1. Download the installation package named Path and double-click it. --> [append the Python installation directory to the variable value. Use; Separate]
Linux:
1. unzip the tar xf Python-3.6.2.tgz2. compile and install cd Python-3.6.2. /configure -- prefix =/usr/local/Python3.6makemake install3. configure the environment variable vim/etc/profilePATH =/usr/local/Python3.6/bin: $ PATHsource/etc/profile4. test python3.6

 

3. The first program (hello, world)

Open pycharm, create a python project, and create a python file.

#!/usr/binl/env python#encoding: utf-8#author: YangLeiprint("hello,world")

 

Iv. Variables

A variable is derived from mathematics. It can store computing results or abstract concepts that can represent values in computer languages. Variables can be accessed by the variable name. In a script language, variables are usually variable, but in a pure functional language (such as Haskell), variables may be immutable. In some languages, variables may be explicitly expressed as mutable states and Abstract With storage space (for example, in Java and Visual Basic ); however, some other languages may use other concepts (such as objects in C) to reference this abstraction, rather than strictly defining the exact extension of "variables.

Declare Variables

#!/usr/binl/env python#encoding: utf-8#author: YangLeiname = "yanglei"print(name)

The code above declares a variable named: name. The value of the variable name is: "yanglei"

Rule for variable definition:

    • Variable names can only be any combination of letters, numbers, or underscores
    • The first character of the variable name cannot be a number.
    • The following keywords cannot be declared as variable names ['and', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif', 'else', 'effect', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import ', 'in', 'is, 'lambda ', 'not', 'or', 'pass', 'print ', 'raise', 'Return ', 'try ', 'while', 'with', 'yield ']

 

V. user input
#!/usr/binl/env python#encoding: utf-8#author: YangLeiinput_name = input("Please enter your name: ")print("hi,%s" % input_name)

 

Vi. Data Types

1. Number

Int (integer)

On a 32-bit machine, the number of digits of an integer is 32 bits and the value range is-2 ** 31 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647 in a 64-bit system, the number of digits of an integer is 64 bits, and the value range is-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807 long (long integer) is different from the C language. The Length Integer of Python does not specify the Bit Width, that is, Python does not limit the size of the long integer, but in fact because the machine memory is limited, the long integer value we use cannot be infinitely large. Note: Since Python2.2, if an integer overflows, Python will automatically convert the integer data to a long integer. Therefore, without the letter L after the long integer data, it will not cause serious consequences. Note: The long type is no longer available in Python3, and all are intfloat (floating point type) Floating point numbers are used to process real numbers, that is, numbers with decimals. Similar to the double type in C, it occupies 8 bytes (64-bit), where 52 bits represent the bottom, 11 bits represent the index, and the remaining one represent the symbol. 2. boolean value true or false 1 or 03, string
"hello world"

String concatenation:

The string in python is embodied in the C language as a character array. Each time you create a string, you need to open up a continuous empty block in the memory. Once you need to modify the string, you need to open up space again, every time the "+" icon appears, it will open up a new space. String formatting output
#! /Usr/binl/env python # encoding: UTF-8 # author: YangLeiname = "yanglei" print ("hi, % s" % name) # output hi, yanglei

Note: The string is % s; integer % d; Floating Point Number % f

Common functions of strings:
  • Remove Blank
  • Split
  • Length
  • Index
  • Slice
4. list creation list:
#!/usr/binl/env python#encoding: utf-8#author: YangLeiname = ["yanglei","jack","marry","andy"]

Basic operations:

  • Index
  • Slice
  • Append
  • Delete
  • Length
  • Slice
  • Loop
  • Include

5. Dictionary (unordered)

Create a dictionary:
#!/usr/binl/env python#encoding: utf-8#author: YangLeiuser_info = {"name":"yanglei","age":23,"job":"IT"}

Common Operations:

  • Index
  • New
  • Delete
  • Key, value, and key-Value Pair
  • Loop
  • Length

 

VII. Data OperationsArithmetic Operation:

Comparison:

Assignment operation:

Logical operation:

Member calculation:

Identity calculation:

Bitwise operation:

Operator priority:

  

8. if judgment

Scenario 1: user login verification

#!/usr/binl/env python#encoding: utf-8#author: YangLeiinput_user = input("Please enter your user name: ")input_password = input("Please enter your password: ")if input_user == "yanglei" and input_password == "123456":    print("\033[32;1m%s login successfully\33[0m" % input_user)else:    print("\033[31;1mThe user name or password error,please try again\033[0m")

Scenario 2: Game of age Prediction

Set your age in the program, and start the program to let the user guess. After the user inputs, the system prompts whether the user inputs correctly. If the input is incorrect, indicates whether to guess whether it is big or small.

#!/usr/binl/env python#encoding: utf-8#author: YangLeiguess_age = 50input_age = int(input("Please enter your guess age: "))if input_age > guess_age:    print("\033[31;1mCan you guess what big\33[0m")elif input_age < guess_age:    print("\033[31;1mCan you guess what small\33[0m")else:    print("\033[32;1mYou guessed it\33[0m")

Outer variable, which can be used by the inner code

Inner variables, should not be used by outer code 9. The difference between break and continue Continue:
#!/usr/binl/env python#encoding: utf-8#author: YangLeicount = 1while count <= 10:    if count == 5:        count += 1        continue    print(count)    count += 1
Break:
#!/usr/binl/env python#encoding: utf-8#author: YangLeicount = 1while count <= 10:    if count == 5:        count += 1        break    print(count)    count += 1

From this we can see that the continue jumps out of the current loop, while the break jumps out of the current loop.

 

10. while loop statement, a basic loop mode of the computer. When conditions are met, the system enters the loop and does not jump out. The general expression of the while statement is: while (expression) {loop body}

Scenario 1: user login verification upgrade

#!/usr/bin/env pyhon#encoding: utf-8#auth: yangleicount = 0while count < 3:    input_user = input("Please enter your user name: ")    input_password = input("Please enter your password: ")    if input_user == "yanglei" and input_password == "123456":        print("\033[32;1m%s login successfully\33[0m" % input_user)        break    elif count == 2:        print("\033[31;1mThe user name or password mistake,three chances to use up,the program exits\33[0m")        break    else:        count += 1        print("\033[31;1mThe user name or password error,please try again\033[0m")

Scenario 2: Game upgrades

 

#!/usr/bin/env pyhon#encoding: utf-8#auth: yangleiguess_age = 50count = 0while count <= 3:    if count == 3:        input_choose = input("Do you want to continue to play?(Y or y|N or n)")        if input_choose == "Y" or input_choose == "y":            count = 0            continue        elif input_choose == "N" or input_choose == "n":            break        else:            print("\033[31;1mAre you input errors!\33[0m")            continue    input_age = int(input("Please enter your guess age: "))    if input_age > guess_age:        print("\033[31;1mCan you guess what big\33[0m")        count += 1    elif input_age < guess_age:        print("\033[31;1mCan you guess what small\33[0m")        count += 1    else:        print("\033[32;1mYou guessed it\33[0m")        break

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.