1. Data types and variables
# Data Type:# 1. Integer: Integers of any size can be processed in PythonintNum1 = 584520 intNum2 = -100 score = 100 # 2. Floating point number: that is, decimal numberfloatNum1 = 3.1415926 floatNum2 = 1.23e10 # Scientific notation# 3. String: Text enclosed with single or double quotesstr1 = '584520JD' str2 = "584520JD" str3 = "Hello Python." # 4. Escape characters: \nBreak, \tTab, \\ for itselfprint("Hello Willard.\n") print("Welcome to FUXI Technology.") # 5. Boolean: True and Falsebool1 = True bool2 = 2 > 1 # Output True# 6. Null value: None, and 0 are not equivalent# Variables and constants:# a. Variable naming: Variable names are a combination of upper and lower case letters, numbers and _, but cannot start with numbers;# b. Format: variable_name = variable_value, equal sign = is the assignment symbol;# c. Constant: variable that cannot be changed, usually using all capital variable names to represent constantsPI = 3.1415926
# Example 1:# Example:student_name = input("Please enter your name:") score = int(input("Please enter your score(0-100):")) NAME = "willard" if student_name == NAME: if ((score > 100) or score < 0): print("The score you entered is wrong! Please re-enter!") else: print("Your score is %d" % score) else: print("Your name is entered incorrectly, please re-enter!")
# Case 1: The input is completely correct outputPlease enter your name:willard Please enter your score(0-100):100 Your score is100 # Case 2: Output with incorrect name inputPlease enter your name:Willard Please enter your score(0-100):59 Your name is entered incorrectly,Please re-enter! # Case 3: The output with incorrect score inputPlease enter your name:willard Please enter your score(0-100):101 The score you entered is incorrect!Please re-enter!
2. String and formatting
# string:# 1. String definition: enclose in single or double quotes;# 2. Get the integer representation of characters: ord() function;# 3. Convert the encoding to the corresponding characters: chr() function;# 4. The type of string is: str;The string type in # is str, represented by Unicode in memory, and one character corresponds to several bytes;# 6. If you want to transfer or save it to disk on the network, you need to turn str into bytes in bytes;# 7. The str represented by Unicode is encoded as specified bytes through the encode() method;# 8. If you read byte streams from the network or disk, the data you read is bytes; you need to use the decode() method to turn bytes into str;# 9. Calculate the string length: len() function;# 10. Tell Linux system that this is a Python executable function, add the following line to the program header#!/usr/bin/env python3 # 11. Tell the Python interpreter to read the source code according to utf-8 encoding, and add the following line to the program header#-*- coding:utf-8 -*-
#Practical combat 1:# 1. String definitionstudentOne = 'Willard' studentTwo = "ChenJD" print("The name of first student is:",studentOne) print("The name of second student is:",studentTwo) print("----------------------------------------------") # 2. Get the integer representation of characters: ord() functionchar1 = 'A' print("The integer of the character A is represented as:",ord(char1)) print("----------------------------------------------") # 3. Convert the encoding to the corresponding characters: chr() functionint1 = 97 print("97 corresponding characters are:",chr(int1)) print("----------------------------------------------") # 4. String type: type() functionstudentName = "FUXI" print("The type of string is:",type(studentName))
# Output result:
The name of first student is: Willard
The name of second student is: ChenJD
----------------------------------------------
The integer of the A character is represented as: 65
----------------------------------------------
The corresponding characters of 97 are: a
----------------------------------------------
The type of string is: <class 'str'>
#Practical combat 2:# 5. Turn str into bytes in bytesprint("'Willard' becomes bytes in bytes:",'Willard'.encode('ascii')) print("'China' becomes bytes in bytes:",'China'.encode('utf-8')) print('----------------------------------------------------------------') # 6. Turn bytes into strprint("b'Willard' changed from bytes to str:",b'Willard'.decode('ascii')) print("b'\\xe4\\xb8\\xad\\xe5\\x9b\\xbd' changed from bytes to str:",b'\xe4\xb8\xad\xe5\x9b\xbd'.decode('utf-8'))
# Output result:
'Willard' becomes bytes in bytes: b'Willard'
'China' becomes bytes in bytes: b'\xe4\xb8\xad\xe5\x9b\xbd'
----------------------------------------------------------------
b'Willard' changed from bytes to str: Willard
b'\xe4\xb8\xad\xe5\x9b\xbd' changed from bytes to str: China
# Practical 3: Confession secret wordsyourWord = input("Please enter the words you want to convert to secret words:") cryptolalia = ('utf-8') print("Your secret word has been generated, please check it!\n",cryptolalia)
# Output result:Please enter the words you want to convert to secret language:I love you,Chen Jindi Your secret word has been generated,Please check! b'\xe6\x88\x91\xe7\x88\xb1\xe4\xbd\xa0\xef\xbc\x8c\xe9\x99\x88\xe9\x87\x91\xe5\xa8\xa3' ---------------------------------- Please enter the words you want to convert to secret language:I love you,China! Your secret word has been generated,Please check! b'\xe6\x88\x91\xe7\x88\xb1\xe4\xbd\xa0\xef\xbc\x8c\xe4\xb8\xad\xe5\x9b\xbd\xef\xbc\x81'
#Practical combat 4:# 6. Calculate string lengthstrOne = "Hello,Welcome to FUXI Technology." strOneLen = len(strOne) print("The length of the string %s is:"%(strOneLen)) print("-------------------------------------------") # Compare the lengths of two namesnameOne = input("First classmate, please enter your name:") nameTwo = input("Second classmate, please enter your name:") nameOneLen = len(nameOne) nameTwoLen = len(nameTwo) if (nameOneLen > nameTwoLen): print("The first classmate's name is longer!") elif (nameOneLen == nameTwoLen): print("The names of the two classmates are the same long!") else: print("The second classmate's name is longer!")
# Output result:
The length of string 33 is:
-------------------------------------------
The first classmate, please enter your name: Willard
The second classmate, please enter your name: ChenJD
The first classmate's name is longer
# Format:# Format method 1:# % is used to format strings# Inside the string, %s means replaced with a string, and %d means replaced with an integer# How many %? placeholders are there, and they need to be followed by several variables or values, which need to be corresponding.# Common placeholders:# %d: integer placeholder; %f: floating point placeholder;# %s: string placeholder; %x: hexadecimal integer placeholder;# %%: means one %;# Format method 2:# format(): Replace the placeholders {0}, {1}, and sequentially in the string with the passed parameters...
#Practical combat 5:print("The following is the personal information registration form, please fill in it truthfully!") print("------------------------------------") name = input("Please enter your name:") sex = input("Please enter your gender(male/female):") age = int(input("Please enter your age:")) qq = input("Please enter your QQ number:") print("------------------------------------") print("Please check again whether the following information is filled in correctly!") print("Your name is: %s"%name) print("Your gender is: %s"%sex) print("Your age is: %d"%age) print("Your QQ number is: %s"%qq) print("-------------------------------------") print("If the above information is correct, please submit it, thank you for filling it out!")
# Output result:
Below is the personal information registration form, please fill in it truthfully!
------------------------------------
Please enter your name: Willard
Please enter your gender (male/female): Male
Please enter your age: 18
Please enter your QQ number: 1107152666
------------------------------------
Please check again whether the following information is filled in correctly!
Your name is: Willard
Your gender is: Male
Your age is: 18
Your QQ number is: 1107152666
-------------------------------------
If the above information is correct, please submit it. Thank you for filling it out!
print("The following is the personal information registration form, please fill in it truthfully!") print("------------------------------------") name = input("Please enter your name:") sex = input("Please enter your gender(male/female):") age = int(input("Please enter your age:")) qq = input("Please enter your QQ number:") print("------------------------------------") print("Please check again whether the following information is filled in correctly!") print("Your name is:{0};gender:{1}".format(name,sex)) print("Your age is:{0};QQNumber:{1}".format(age,qq)) print("-------------------------------------") print("If the above information is correct, please submit it, thank you for filling it out!")
#Result output:
Below is the personal information registration form, please fill in it truthfully!
------------------------------------
Please enter your name: Willard
Please enter your gender (male/female): Male
Please enter your age: 18
Please enter your QQ number: 1107152666
------------------------------------
Please check again whether the following information is filled in correctly!
Your name is: Willard; Gender: Male
Your age is: 18; QQ number: 1107152666
-------------------------------------
If the above information is correct, please submit it. Thank you for filling it out!
Note: The above code has been verified, but it is not the code deployed in the production environment. It is just some small demos to explain the relevant knowledge of Python. Please skip it!
Summarize
That’s all for this article. I hope it can help you, and I hope you can pay more attention to more of my content!