程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Data type and formatted output (Python)

編輯:Python

data type

# Functions for data type viewing type( data / Variable name )
# Basic data type :int float bool
# Container type : str list tuple set dict
# integer 
int1 = 12
print(type(int1)) # <class 'int'>
# floating-point 
float1 = 12.1
print(type(float1)) # <class 'float'>
# Boolean type (True/False)
bool1 = True
print(type(bool1)) # <class 'bool'>
# String type 
str1 = 'hello Python'
print(type(str1)) # <class 'str'>
# Tuples 
tuple1 = (1, 2, 3, 4)
print(type(tuple1)) # <class 'tuple'>
# list 
list1 = [1, 2, 3, 4]
print(type(list1)) # <class 'list'>
# aggregate 
set1 = {
1, 2, 3, 4}
print(type(set1)) # <class 'set'>
# Dictionaries 
dict1 = {
'name': 'xiaoming', 'age': 18}
print(type(dict1)) # <class 'dict'>

Format output

  • Formatting is a function of strings , And print irrelevant , Even without output , You can also format strings
# String formatting : Formatting is a function of strings 
# print Output : print Function can only display the incoming content to the console , It has nothing to do with formatting 
# demand : Want Xiao Ming's age , follow age Changes in variables , Constantly changing , So what should we do ?
age = 16
print(' Xiao Ming 14 year ')
# Formatting of strings 
# Format output , What is the print The function of string is also the function of string ?
print(' Xiao Ming %d year ' % age)
# Explore 
str1 = ' Xiao Ming %d year ' % age
print(str1)
  • Format :
    • Single placeholder :‘ What to write , Place holder ’ % Variable name
    • Multiple placeholders : ‘ What to write , Place holder 1, Place holder 2, ....’ % ( Variable 1, Variable 2,....)
      • % The number of previous placeholders should be the same as % After that, the number of variables matches , 11. If it is correct, it will report an error
# Format : ' character string , Place holder ' % Variable 
# In the above format , After formatting , The placeholder position will be filled with the corresponding variable 
# Variables of different data types , Use different placeholders for placeholders 
# String data use %s
# Floating point data use %f
# Integer data use %d
name = 'xiaoming'
age = 18
height = 1.85
weight = 69.5
marriage = False
# Formatted output of a placeholder 
print(' The name of the trainee is %s' % name)
print(' The student's age is %d' % age)
print(' The student's height is %f' % height)
print(' The weight of the student is %f' % weight)
print(' The student's marital status is %s' % marriage)
# When there are multiple dynamic variables , We need to use multiple placeholders for placeholders 
# TypeError: not enough arguments for format string
# If there are multiple placeholders in front , The following variables should be wrapped in parentheses 
print(' The name of the trainee is %s, The student's age is %d year , The student's height is %f rice , The weight of the student is %fkg, The student's marital status is %s' % (name, age, height, weight, marriage))
# The number of variables in brackets cannot be less than the number of placeholders 
# print(' The name of the trainee is %s, The student's age is %d year , The student's height is %f rice , The weight of the student is %fkg, The student's marital status is %s' % (name, age, height, weight))
# not all arguments converted during string formatting
# The number of variables in parentheses cannot be more than the number of placeholders 
# print(' The name of the trainee is %s, The student's age is %d year , The student's height is %f rice , The weight of the student is %fkg, The student's marital status is %s' % (name, age, height, weight,marriage,name))
# Conclusion : Number of placeholders , And % The number of variables after must be consistent , If it is a placeholder , You can use a variable , If there are multiple placeholders , So many variables must be wrapped in parentheses 
# Can you control the style of the output of variables : Sure 
name = 'xiaoming'
age = 18
height = 1.85
weight = 69.5
id = 12
# demand :1. Keep two decimal places for height , Keep the weight to three decimal places 
# demand :2. Student's id Co occupation 6 position , It's not enough 0 fill 
# Use ctrl + d You can copy the whole line 
print(' The name of the trainee is %s, The student's age is %d year , The student's height is %f rice , The weight of the student is %fkg, Student number is %d' % (name, age, height, weight, id))
# Floating point reserved n Decimal place : %.nf
# Integer occupancy n Bit data , It's not enough 0 A filling %0nd
print(' The name of the trainee is %s, The student's age is %d year , The student's height is %.2f rice , The weight of the student is %.3fkg, Student number is %06d' % (name, age, height, weight, id))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved