Python BASICS (2) variables, process control, and python Variables

Source: Internet
Author: User

Python BASICS (2) variables, process control, and python Variables
I. Variables

Declare variables:Name = "Henry" # variable name: name, variable name value: "Henry"

The role of a variable: nickname, which represents the content stored in an address in the memory.

Variable definition rules:

  • Variable names can only be any combination of letters, numbers, or underscores.
  • The first character of the variable name cannot be a number.
  • Special keywords cannot be declared as variable names (for example, 'and', 'LIST', 'not ', 'for', 'while '.........)

To better understand the location of a variable in the memory, the following example is provided:

1 >>> x = 1 2 >>> y = x 3 >>> id (x) 4 10455040 5 >>> id (y) ## id () variable is to view the memory address of your variable, 6 10455040 7 >>> x = 2 8 >>> id (x) 9 1045507210 >>> id (y) 11 10455040View Code

The reason for the above results is that all strings stored in the memory in python are stored in character arrays, because the character arrays in the memory are continuous, so once the string is modified, it is re-created, when a change occurs, a new memory is used in the memory.

1. If you use concatenated strings in python, a lot of memory will be wasted. 2 Examples: 3 4 'hello' + 'World' + '!!!! In this case, '5 will first open up an address for 'hello' in the memory, then open a address for 'hello' + 'World', and finally open a address '!!! 'Address, a waste of memory, the so-called evil' + 'number.View Code 2: Input and Output

The modules used in python2. * And python3 are different. In python2. *, raw_input and input are used only. In python3, only input is used.

1 # In python2. * 2> name = raw_input ('Please input your name: ') 3 please input your name: henry 4 >>> print name 5 henry 6 7 >>> name = input ('Please input your name: ') 8 please input your name: henry 9 Traceback (most recent call last): 10 File "<stdin>", line 1, in <module> 11 File "<string>", line 1, in <module> 12 NameError: name 'henry' is not defined13 14 >>>> name = 'henry' 15 >>> UserName = input ('Please input your name :') 16 please input your name: name17 >>> print UserName18 henryView Code1 >>> name = raw_input ('raw _ input: ') 2 raw_input: 1233 >>> type (name) 4 <type 'str'> 5 >>> name = input ('input: ') 6 input: 1237 >>> type (name) 8 <type 'int'>View Code

The code above shows that raw_input can be input and output normally. When you use input, an error is returned when you directly input a string, because it treats user input as a variable, the string is enclosed in quotation marks and can be correctly assigned values. The second example shows the differences between raw_input and input.

In python3, the input content is processed by string.

1 >>> name = input ('Please input your name: ') 2 please input your name: test3 >>> type (name) 4 <class 'str'> 5 >>> name = input ('Please input your name: ') 6 please input your name: 1231237 >>> type (name) 8 <class 'str'>View Code

In our life, many website login passwords are invisible. The following describes a module (getpass) that hides user input)

1 >>> import getpass2 >>> passwd = getpass. getpass ('input passwd: ') 3 input passwd: 4 >>> print (passwd) 5 alex3714View Code 3, process control 1, if... else condition judgment
1 name = input ('enter Username: ') 2 3 if name = 'jack': # Here' = 'is a comparison of two values, it is not the same object in the memory. If it is, it must be the same address in the memory, rather than the 4 print ('login successfully') 5 else: 6 print ('logon failed ')
2. if... elif... else multi-branch condition judgment
1 name = input ('enter your Username: ') 2 3 if name = 'jack': 4 print ('ordinary member') 5 elif name = 'bob ': 6 print ('gold member ') 7 elif name = 'henry': 8 print ('Diamond member') 9 else: 10 print ('invalid user ')
4. Loop 1, for Loop

Here are two examples:

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 name_list = ['jack', 'bob', 'Eric ', 'six'] 5 6 for ele in name_list: 7 if ele = 'jack': 8 print ('hell0, jack !! ') 9 continue # if the continue condition is true, exit the current loop and continue the next loop 10 if ele = 'six': 11 print ('hello, six !!! ') 12 break # exit the loop directly when the break condition is true
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 for I in range (3 ): # define three cycles 5 user_input = raw_input ("Your username:") # user input username, password 6 passwd = raw_input ("Your password :") 7 if user_input = valid_user and passwd = valid_passwd: 8 print "Welcome % s login to our system! "% User_input 9 break10 elif user_input = 'guest ': 11 print" Welcome % s login our system, but you only haveread-only access, enjoy! "12 break13 else: 14 print" invalid username! "15 else: 16 print" You 've retried 3 times, to avoid attack, I will block your IP address .."
2. while Loop
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 Num1 = 0 5 Num2 = 0 # define two counters 6 7 8 while True: 9 Num1 + = 110 print ('num1: ', Num1) 11 break_flag = False # define an exit flag in the parent loop. If the child loop needs to jump out together with the parent loop, set the exit flag to True12 while True: 13 Num2 + = 114 if Num2 = break_flag = True # The parent loop jumps out of 16 break together # Jump out of the first layer 17 print ('num2: ', Num2) 18 if break_flag: # determine whether the Sub-loop has jumped out, and then jump out 19 print ('exit .... ') 20 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.