You're trying to pretend that you're deceiving only yourself.
Variables and types
In programming, variables are a carrier of data.
Several Data Types
· Integers (int): Any size of integers can be processed in python.
· Floating point type: that is, decimal.
· String type (str): Any text enclosed in single or double quotation marks.
· Boolean: Only true and False (in python, all but 0 and None represent True).
· Complex type: 3 + 4j.
Input () renturn is a string.
Variable naming
Hard rules:
· Variable names are composed of letters, numbers and underscores. Numbers cannot begin.
· Case sensitive.
· Do not conflict with keywords and system reserved words (such as functions, modules, etc.).
PEP 8 requires:
· Pinyin in lowercase letters and underline multiple words.
· Protected column attributes begin with a single underscore.
· Private real column attributes begin with two underscores.
· Every colon must be indented.
Format output
1.% is the most connected,% s,% d,% f:% 2.f reserves decimal digits.
Example print('% f +% f =% 2.f'% (num, num1, num + num1))
3. format
Example print ('{} + {}= {}'. format (num, num1, num + num1)).
Retain decimal digits: print ('{:.2f} + {:.2f}= {}'.format (num, num1, num + num1))
Type Conversion
int (): Converts a numeric value or string to an integer, specifying a base.
float (): Converts a string to a floating point number.
str (): Converts the specified object to a floating point number.
chr (): Converts an integer into a string corresponding to the encoding (a character).
ord (): Converts a string (a character) to its corresponding encoding (integer).
Requirement: Encrypt the input zip code using ASCII.
email = input("input your email:") for i in email: ord_ = ord(i) ord1 = ord_ + 10 str_ = chr(ord1) print(str_,end=" ")
Advanced: Use python's md5 to complete.
operator
[], subscript [:], slice [start (start): end (end): step (step)]
In: If a value is found in the specified sequence, it returns true, otherwise it returns false.
not in: Returns true if no value is found in the specified sequence, otherwise false.
Is, is not identity operator
The difference between is and ==:
python contains three basic elements: id (identity), type (data type), value (value). Is compares whether the id values of two objects are equal, that is, whether two objects are the same real object and whether they point to the same memory address. == The comparison is whether the contents of the two objects are equal.
Exercise: Enter a number to determine if it's a narcissus or not.
number = input('number:>>') if len(number) > 3: print('[!] Error, The lenghts must be Three!!') else: bai = int(number[0]) shi = int(number[1]) ge = int(number[2]) if int(number) == bai ** 3 + shi **3 + ge **3 : print(number %'Narcissus') else: print(number %'Not Narcissus')
Example: Extract the address in the following string (I used only one method. split())
You can also use regular expressions, slices, etc.
import re str1 = '<li class="bold-item"><a href="http://Baijiahao.baidu.com/s?Id=1641806920856311184"target="_blank"mon="a=9">Cai Chongxin's total 2.35 billion wholly-owned acquisition of the Nets </a> </li>" str2 = '<li><a href="http://Baijiahao.baidu.com/s?Id=16418011342707690"target="_blank"mon="a=9">The United States prohibits some Apple MacBook Pro boarding </a></li>" str3 = '<li><a href="http://Baijiahao.baidu.com/s?Id=1641804985134705772"target="_blank"mon="a=9">The board of Qualcomm appointed Mark McLaughlin as chairman </a> </li>" str4 = '<li><a href="http://Baijiahao.baidu.com/s?Id=1641795161667151424"target="_blank"mon="a=9">Silicon Valley giants will attend US government hearings against digital tax </a></li>" str5 = '<li><a href="http://Baijiahao.baidu.com/s?Id=1641759706552362036"target="_blank"mon="a=9">5G mobile phone"shop"users are still watching </a> </li>" print(str1.split('"')[3]) print(str2.split('"')[1]) print(str3.split('"')[1]) print(str4.split('"')[1]) print(str5.split('"')[1])
Branch structure
In python, you can use if, elif, else keywords to construct branching structures
Be careful
In if, elif, it must be = sign, no = sign.
Guess the Number Passing Game
import random count = 0 random1 = random.randint(1,5) print(random1) random2 = random.randint(1,5) print(random2) number = int(input('Enter the sum of your guesses and:')) for i in range(10): if number == random1 + random2: print('Congratulations on getting 100 points') count+=100 else: print('Dial the wrong number') if count == 1000: print('Congratulations on passing the second pass') else: print('Game,Over')
Exercise: Scissors, Stones, Cloth
import random con = random.randint(0,2) user = int(input('0:Stone, 1: scissors, 2: cloth')) if con == user: print("It ends in a draw") else: if con == 0 and user == 1: print("The computer won") elif con == 1 and user == 2: print("The computer won") elif con == 2 and user == 1: print("The computer won") else: print("Players won")