Python-Day1 Python basics, python-day1python

Source: Internet
Author: User
Tags windows download

Python-Day1 Python basics, python-day1python
1. Install Python 3.5.x 1. Windows

On Windows, search for "Python for windows download" and click "OK". During installation, you can select "set environment variables" or "manually set" after installation, in this way, you can directly enter the command in cmd to use it.

2. Linux

By default, Python is available in Linux. It depends on how you install Python. Centos7 is python2.7.x by default. You can enter python-V in the command line to display the version. So you don't have to worry about the Python dependency package. If you have this problem, du Niang can solve it. If you don't talk about it, go directly to download Python3.5.x for installation.

 

Wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz # You can first play the front page to see, select the package to install and then download. Tar-zxf Python-3.5.2.tgzcd Python-3.5.2. /configure -- prefix =/usr/local -- enable-sharedmakemake installln-s/usr/local/bin/python3/usr/bin/python3echo/usr/local/lib>/etc /ld. so. conf. d/local. conf # configure the library before running. It is unavailable after installation. Run these two commands. Refresh the dynamic library. Ldconfigpython-V (python3 -- version)
Ii. Learning ceremony-Hello world !!!
1 print ("Hello world !!!") # This is a language learning ceremony O (strong _ strong) O Haha ~
1 #! /Usr/bin/env python2 #-*-coding: UTF-8-*-3 print ("Hello, world !!!")

The first line is to specify the interpreter. If the interpreter is not specified,./hello. py is incorrect and must be executed in python hello. py. #! /Usr/bin/python and #! The difference between/usr/bin/env python is that the former is equivalent to the location where the python interpreter is dead, and the latter finds the python interpreter under the environment variable, that is #! /Usr/bin/python will only find the python interpreter under/use/bin, and the other will find python in all the environment variables. If python is not available under/usr/bin/and env has/usr/bin/and/usr/local/bin/, then #! /Usr/bin/env python will use the/usr/local/bin/python interpreter, and #! /Usr/bin/python cannot find the interpreter.

If there is an ASCII code error in the second line without hitting Python2, it should be declared that UTF-8 encoding is used.

Iii. Variable and variable Assignment 1. variable naming rules
  • Camper

Camper: ConsumerUserName indicates the upper-case letter of each word.

  • Subscript

Subscript: consumer_user_name indicates that each word is separated by an underscore.

  • Variables that cannot be used (keywords reserved by python)

['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']

  • Set a variable that you do not want to change. We can use all uppercase variables, such as PIE = 3.141592653.
2. Variable assignment

When a variable is created and a value is assigned to the variable, the computer performs the following operations: opening up a memory space cheng and directing the Name variable to the memory space, when this Name variable is assigned to another variable Name2, it indicates that the Name2 variable points to the memory space of the cheng instead of the Name. Therefore, when the Name changes, the Name2 value does not change.

>>> Name = 'cheng' # first open up a space in the memory and point the variable to Name >>> id (Name) # Check the memory address of this space. For example, 51403272> Name2 = Name # We also point the variable Name2 to the variable Name> id (Name2) 51403272 >>> Name = 'er' # Open up the second space and point the Name variable to the new memory space >>> id (Name) # view the memory address pointed to by Name 51335216 >>> id (Name2) # view the memory address 51403272 pointed to by Name2

 

Iv. Input and Output
Name = input ("name:") # input #2. raw_input in x is 3. x is no longer needed. 3. x Default input character format print (type (name) # output name type age = int (input ("age :")) # forced conversion to int type print (type (age) job = input ("job:") salary = input ("salary :") # formatting the output in placeholder mode: info = ''' -------- info of % s ----- Name: % sAge: % dJob: % sSalary: % s ''' % (name, name, age, job, salary) # specify the formatting output of the variable name and value info2 = ''' -------- info2 of {_ Name} ----- name: {_ name} Age: {_ age} Job: {_ job} Salary: {_ salary }'''. format (_ name = name, _ age = age, _ job = job, _ salary = salary) # formatting output info3 = ''' -------- info3 of {0} ----- Name: {0} Age: {1} Job: {2} Salary: {3 }'''. format (name, age, job, salary) print (info, info2, info3) ''' here is the output result of the annotation. This is true for multi-line annotations, the single row is "#" name: root <class 'str'> age: 23 <class 'int'> job: ITsalary: 6666666 -------- info of root ----- Name: rootAge: 23Job: ITSalary: 6666666 -------- info2 of root ----- Name: rootAge: 23Job: ITSalary: 6666666 -------- info3 of root ----- Name: rootAge: 23Job: ITSalary: 6666666 '''
Import getpasspasswd = getpass. getpass ('Please input your passwd: ') print (passwd) ''' is a hidden password. The running result is as follows: C: \ Users \ CHENG \ PycharmProjects \ untitled> python B. pyplease input your passwd: 123 '''

 

V. Process Control

In python, forcible indentation is used to differentiate process control. If it is a parent-level statement, it must be written in the top level.

1. if, if... else, if... elif... else judgment

2. while Loop

3. for Loop

Use the following code to record the above three process control structures:

user = 'cheng'passwd = '123'user_input = input("Your username:")passwd_input = input("Your password:")if user_input == user and passwd_input == passwd:    print ("Welcome %s login to our system!" %user_input)elif user_input == 'guest':    print ("Welcome %s login to our system!,but you only have read_only access,enjoy!" %user_input)else:    print ("Incalid username!Byebye!")
age_of_cheng = 23count = 0while count <3:    guess_age = int(input("guess age:") )    if guess_age == age_of_oldboy :        print("yes, you got it. ")        break    elif guess_age > age_of_oldboy:        print("think smaller...")    else:        print("think bigger!")    count +=1else:    print("you have tried too many times..fuck off")
age_of_cheng = 23for i in range(3):    guess_age = int(input("guess age:") )    if guess_age == age_of_oldboy :        print("yes, you got it. ")        break    elif guess_age > age_of_oldboy:        print("think smaller...")    else:        print("think bigger!")else:    print("you have tried too many times..fuck off")
The logon interface code. The following is the flowchart 1 import random 2 user = ['root', 'admin', 'others '] 3 passwd = ['1', '2 ', 'o'] 4 user_input = input ("Enter your Username:") 5 # determine whether the user exists or is locked 6 f1_open('deny.txt ', 'A ') 7 d1_open('deny.txt '). read () 8 if user_input in user: 9 print ("Welcome % s use logon interface" % user_input) 10 if user_input in d: 11 print ("However, the % s user is locked, exiting... "% User_input) 12 exit () 13 else: 14 print (" % s user does not exist, exiting... "% User_input) 15 exit () 16 # judge whether the user password is correct, and lock 17 I = 018 while I three times in a loop (" enter your password: ") 20 if passwd_input = passwd [user. index (user_input)]: 21 print ("Logon successful, welcome") 22 break23 else: 24 print ("input error") 25 I ++ = 126 else: 27 print ("too many input errors, user name locked") 28 # f1_open('deny.txt ', 'A') 29 f. write (user_input) 30 f. write (',') 31 f. close ()

 

 

The following are common examples:

The wood of the hug, born at the end of the ml; the Nine-layer platform, starting from the ground; a journey of thousands of miles, began in the foot. ------- Lao Tzu

 

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.