Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the data types, variables, strings and formatting of Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly analyzes how to analyze the data types, variables, strings and formatting of Python. The content is detailed and easy to understand, and the operation details are reasonable, so it has a certain reference value. If you are interested, you might as well follow the editor and learn more about "how to analyze the data types, variables, strings and formatting of Python".

1. Data types and variables # data types: # 1. Integer: Python can handle any size of integer intNum1 = 584520intNum2 =-100score = 10. 2. Floating point number: that is, decimal floatNum1 = 3.1415926floatNum2 = 12.3 billion # Scientific notation # 3. String: text str1 = '584520JD'str2 = "584520JD" str3 = "Hello Python." # 4 enclosed in single or double quotes. Escape character:\ nnewline,\ t tab,\\ is print ("Hello Willard.\ n") print ("Welcome to FUXI Technology.") # 5. Boolean value: True and Falsebool1 = Truebool2 = 2 > 1 # output True# 6. Null: None, and 0 are not equivalent # variables and constants: # a. Variable naming: the variable name is a combination of uppercase and lowercase letters, numbers and _, but cannot start with a number; # b. Format: variable_name = variable_value, equal sign = is the assignment symbol; # c. Constant: variable that cannot be changed, usually denoted by all uppercase variable names: PI = 3.141592instance # instance: student_name = input ("Please enter your name:") score = int ("Please enter your score (0100):") NAME = "willard" if student_name = NAME: if ((score > 100) or score

< 0): print("您输入的分数错误!请重新输入!") else: print("您的分数为%d" % score)else: print("您的名字输入有误,请重新输入!")# 情况1:输入完全正确的输出请输入您的名字:willard请输入您的分数(0-100):100您的分数为100# 情况2:名字输入有错的输出请输入您的名字:Willard请输入您的分数(0-100):59您的名字输入有误,请重新输入!# 情况3:分数输入有错的输出请输入您的名字:willard请输入您的分数(0-100):101您输入的分数错误!请重新输入!2.字符串和格式化# 字符串:# 1.字符串定义:用单引号或双引号括起来;# 2.获取字符的整数表示:ord()函数;# 3.把编码转换为对应的字符:chr()函数;# 4.字符串的类型为:str;# 5.Python中的字符串类型为str,在内存中以Unicode表示,一个字符对应若干个字节;# 6.如果要在网络上传输或保存到磁盘上,需要把str变成以字节为单位的bytes;# 7.以Unicode表示的str通过encode()方法编码为指定的bytes;# 8.如果从网络或磁盘上读取字节流,读到的数据为bytes;需要使用decode()方法把bytes变为str;# 9.计算字符串长度:len()函数;# 10.告诉Linux系统,这是一个Python可执行函数,在程序头添加下面一行#!/usr/bin/env python3# 11.告诉Python解释器,按照utf-8编码读取源代码,在程序头添加下面一行#-*- coding:utf-8 -*-# 实战1:# 1.字符串定义studentOne = 'Willard'studentTwo = "ChenJD"print("The name of first student is:",studentOne)print("The name of second student is:",studentTwo)print("----------------------------------------------")# 2.获取字符的整数表示:ord()函数char1 = 'A'print("A字符的整数表示为:",ord(char1))print("----------------------------------------------")# 3.把编码转换为对应的字符:chr()函数int1 = 97print("97对应的字符为:",chr(int1))print("----------------------------------------------")# 4.字符串类型:type()函数studentName = "FUXI"print("字符串的类型为:",type(studentName)) # 输出结果: The name of first student is: Willard The name of second student is: ChenJD ---------------------------------------------- A字符的整数表示为: 65 ---------------------------------------------- 97对应的字符为: a ---------------------------------------------- 字符串的类型为: # 实战2:# 5.把str变成以字节为单位的bytesprint("'Willard'变成以字节为单位的bytes:",'Willard'.encode('ascii'))print("'中国'变成以字节为单位的bytes:",'中国'.encode('utf-8'))print('----------------------------------------------------------------')# 6.把bytes变为strprint("b'Willard'从bytes变为str:",b'Willard'.decode('ascii'))print("b'\\xe4\\xb8\\xad\\xe5\\x9b\\xbd'从bytes变为str:",b'\xe4\xb8\xad\xe5\x9b\xbd'.decode('utf-8')) # 输出结果: 'Willard'变成以字节为单位的bytes: b'Willard' '中国'变成以字节为单位的bytes: b'\xe4\xb8\xad\xe5\x9b\xbd' ---------------------------------------------------------------- b'Willard'从bytes变为str: Willard b'\xe4\xb8\xad\xe5\x9b\xbd'从bytes变为str: 中国 # 实战3:表白密语yourWord = input("请输入您想转换成密语的话:")cryptolalia = yourWord.encode('utf-8')print("您的密语已生成,请查收!\n",cryptolalia)# 输出结果:请输入您想转换成密语的话:我爱你,陈金娣您的密语已生成,请查收! b'\xe6\x88\x91\xe7\x88\xb1\xe4\xbd\xa0\xef\xbc\x8c\xe9\x99\x88\xe9\x87\x91\xe5\xa8\xa3'----------------------------------请输入您想转换成密语的话:我爱你,中国!您的密语已生成,请查收! b'\xe6\x88\x91\xe7\x88\xb1\xe4\xbd\xa0\xef\xbc\x8c\xe4\xb8\xad\xe5\x9b\xbd\xef\xbc\x81'# 实战4:# 6.计算字符串长度strOne = "Hello,Welcome to FUXI Technology."strOneLen = len(strOne)print("字符串%s的长度为:"%(strOneLen))print("-------------------------------------------")# 比较两个名字的长度nameOne = input("第一个同学,请输入您的名字:")nameTwo = input("第二个同学,请输入您的名字:")nameOneLen = len(nameOne)nameTwoLen = len(nameTwo)if (nameOneLen >

NameTwoLen): print ("the first classmate's name is longer!") Elif (nameOneLen = = nameTwoLen): print ("the names of two students are the same length!") Else: print ("the second classmate's name is longer!")

# output result:

The length of the string 33 is:

First classmate, please enter your name: Willard

Second classmate, please enter your name: ChenJD

The name of the first classmate is longer.

# formatting: # formatting method 1Flux #% is used to format the string # inside the string,% s means to replace with a string,% d means to replace # with an integer. How many%? Placeholders, followed by several variables or values, need to correspond to # common placeholders: #% d: integer placeholder;% f: floating point placeholder; #% s: string placeholder;% x: hexadecimal integer placeholder; #%: represents a% # formatting method 2 # format (): replace the placeholders {0}, {1} and. # actual 5:print in the string with the passed parameters ("here is the registration form of personal information, please fill in 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, thank you for filling in!")

# output result:

The following is the registration form of personal information, please fill in 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, thank you for filling in!

Print ("here is the registration form of personal information, please fill in 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}; QQ number: {1}" .format (age,qq)) print ("-") print ("if the above information is correct, please submit, thank you for filling in!")

# result output:

The following is the registration form of personal information, please fill in 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: 18th QQ: 1107152666

-

If the above information is correct, please submit, thank you for filling in!

Note: the above code has been verified, but it is not the code deployed in the production environment, just some small Demo to illustrate the relevant knowledge of Python, please skip it!

What are the main application areas of python? 1. Cloud computing, typical application OpenStack. 2. WEB front-end development, many large websites are developed by Python. 3. Artificial intelligence applications, which are based on big data's analysis and deep learning, have essentially been unable to leave python. 4. System operation and maintenance project, the standard configuration of automatic operation and maintenance is python+Django/flask. 5. Financial management analysis, quantitative transaction, financial analysis. 6. Big data's analysis.

On "how to analyze Python data types, variables, strings and formatting" is introduced here, more related content can search previous articles, hope to help you answer questions, please support the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report