Python journey 1.1 (first knowledge of python), python journey 1.1 first Knowledge
Some basic knowledge that must be mastered before learning python
1. Programming Language
2. python, C # JAVA
3. python: pypy cpython jpython
4. Execution Method
Interpreter
File Execution
5. Specify the interpreter
Python xxx. py
./Xxx. py #! /Usr/bin/env python
Ascii unicode UTF-8
Python
2.7 #-*-coding = UTF-8 !)
3. x uses UTF-8 by default. (For Beginners, it is not normal and I cannot understand it at first, so let's forget it !)
6. program output, print statement and "hello world ".
1 >>>mystring="hello world"2 >>>print(mystring)3 ----------------------------4 hello world5 ----------------------------6 >>>mystring7 'hello world'
Note: (1) the output string is enclosed by single quotes, so that the string object can be output on the screen as a string. -- That is, it displays the string of the object, not just the string itself.
(2) The underline has a special meaning in the interpreter, which can represent the value of the last expression.
>>> _hello world
(3) The print Statement of python can be used with the string operator (%) to replace the string (% s is used to replace the character type, and % d is used to replace the integer type ). (Similar to the printf () function in C .)
But python is flexible. Even if you pass the character type to % d, it will not cause serious consequences.
6. variables, representative
Variable name = Value
Variable name requirements:
Numbers, letters, and underscores
The number cannot start
It cannot be the same as the py keyword.
7. built-in program input and input functions.
First, the built-in functions in version 3.x are directly converted into input () functions.
>>> Name = input ("please input your name:") >>>> print (name) --------------------------------------------------- please input your name: handsome guy
8. condition if
1 if judgment condition execution statement 1 ...... 3 elif judgment condition execute Statement 2 ...... 5 else: 6 execute Statement 3 ......
If yes .... Then execute .... Otherwise, execute...
9. two common methods of annotation.
(1) "#": single line comment.
(2) '''content code''': multi-line comment.
10. while
While condition:
Run from top down
Determines whether the condition is true.
(1) Use a while loop to input 1 2 3 4 5 6 8 9 10
i = 1while i<=10: if i==7: i += 1 else: print(i) i+=1
Ideas:
★If the cycle is less than 10, you only need to judge that the variable I is less than 11 or less than or equal to 10 is true;
★Only one 7 is not output, so if the variable is equal to 7, only add 1 without printing. If the variable is other, print and Add 1.
(2) Calculate the sum of all numbers from 1
1 i = 12 sum = 03 while i<=100:4 sum += i5 i += 16 print(sum)
Ideas:
★For the sum of the numbers less than 100 in a loop, you only need to judge that the I variable is less than 101 or less than or equal to 100 is true;
★The sum of all numbers equals to the sum of values in each loop. After the loop is completed, print and
Output result: 5050
(3) Output all odd numbers in 1-100
1 nub = int (input ("enter an odd number or less than the expected number and :")) # if you enter 100, the int value is the number 2 I = 1 3 sum = 0 4 while I <= nub: 5 if I % 2! = 0: 6 sum + = I 7 I + = 2 8 else: 9 I + = 110 print (sum)
Ideas:
★For the sum of the numbers less than 100 in a loop, you only need to judge that the I variable is less than 101 or less than or equal to 100 is true;
★The odd number is 1, 3, 5, so if the remainder is not equal to 0, the result is an odd number, and the odd number can be added up.
★The previous step has determined that it is an odd number. If you add 2 to each operation, the next odd number is obtained. You do not need to execute else.
Output: 100 is 2500
(4) Output all even numbers in 1-100
1 i = 12 sum = 03 while i<=100:4 if i%2==0:5 sum = sum + i6 i += 27 else:8 i += 19 print(sum)
Ideas:
★For the sum of the numbers less than 100 in a loop, you only need to judge that the I variable is less than 101 or less than or equal to 100 is true;
★If the odd number is 2, 4, 5, divide 1 by 2, and the remainder is equal to 0, it is an even number, and the even number can be added up.
★The previous step has determined that it is an even number. If you add 2 to each operation, the next even number is obtained. You do not need to execute else.
The output result is: the even number within 100 is 2550.
(5) Calculate the sum of all numbers of 1-2 + 3-4 + 5... 99
1 odd,even,i = 0,0,12 while i<100:3 if i%2==0:4 even += i5 i += 16 else:7 odd += i8 i += 19 print(odd-even)
Ideas:
★It can be seen from the requirements that the odd numbers are both positive and the even numbers are both negative. The sum of the odd numbers can be reduced to the sum of the even numbers;
★Three variables: Odd sum, even sum, and numeric variable. If the number of cycles is less than 100, it is true;
★If the remainder of a number is zero except 2, it is an even number, and the sum of an even number is added. Otherwise, it is an odd number, and the sum of an odd number is added. After the loop is completed, the sum of an odd number and an even number is printed.
Output result: 50
1 sum,even,i = 0,0,12 while i<100:3 sum += 14 if i%2==0:5 even += i6 i += 17 else:8 i += 19 print(sum-2*even)
Ideas:
★According to the requirements, we can see that the odd numbers are both added and the even numbers are reduced. We can calculate the sum of 1-99, then subtract the even number to the odd number and then subtract the even number to the result;
★Three variables, sum, even number, and numeric variable. If the number of cycles is less than 100, it is true;
★Calculate the sum between 1 and 99 cyclically, determine whether it is an even number, and calculate the even number and. If it is an odd number, add 1;
★Print the sum and subtract two even numbers to calculate the sum.
Output result: 50
(6) User Login (three chances to retry)
1 name,pswd,i = "hairui" ,"123456",1 2 while i < 4: 3 input_name = input("Username:") 4 input_pswd = input("Password:") 5 if input_name == name and input_pswd == pswd: 6 print("Welcome to my program!") 7 break 8 else: 9 print("The user name or password you entered is incorrect. Please enter again.")10 i += 111 continue12 print("Enter more than 3 times, goodbye!")
Ideas:
★Variable defines the user name, password, number, and number used to determine the number of inputs;
★Determine whether the variable I is less than 3 is true, and allow the user to enter the account and password for interaction;
★If the entered account and password are the same as the variable username and password, the welcome information is output and the break is exited. If not, the output username or password is incorrect and then the variable I + 1 is re-entered, exit the contiune cycle.
★If I = 4, the loop is not executed. The output has been input more than 3 times. Goodbye!