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

Python basics start with Hello World (the next day)

編輯:Python

Today, I know several data type operations


  One . Cyclic else

This is my first contact ,java There is no prototype in it, haha

It means for and while The operation that normally ends after the cycle

str = 'python It's fun '
for i in str:
print(i)
else:
print(' This is executed when the loop ends ')


Two . String operation  

I got it! Python Of '''''' Three quotes

I also know escape characters

# character string
a ='hello'
b = "python"
c = '''world'''
print(type(a))
print(type(b))
print(type(c))
# / escape
qq ='I\'math'
print(qq)


 2.1. Get a word in the specified subscript string

  Slice new words

# Get the subscript of the specified string Subscript from 0 Start
qqq = 'asdfghjkl'
print(qqq[1])
# section
print(qqq[0:3:1])

 2.2. Search a string for a character subscript you want

qqq = 'asdfghjkl'
# Find the subscript of the string you want in a string
print(qqq.find('a'))

 


 2.3. Replace the characters you want to replace as follows. I replaced 1 become 3

# mystr Replace
strs = '1212121212'
print(strs.replace('1','3'))

  


2.4. toggle case

capitalize Only the first letter of the string can be changed

# toggle case Capitalize the first letter
strZhuan = 'qw eqwe qwe'
print(strZhuan.capitalize())


strZhuan Change the capitalization of each initial

strZhuan = 'qw eqwe qwe'
print(strZhuan.strZhuan ())


  Switch all case  

lower Each one is lowercase upper Each one is capitalized
strZhua = 'ADADADAD'
print(strZhua.lower())
# Convert small and medium case strings to uppercase
strZh = 'ADADADAD'
print(strZh.upper())


  Remove white space , String to its

# Delete the blank space to the left of the string
mystr = ' Left side measurement 2 '
print(mystr.lstrip())
# Delete the blank space to the right of the string
mystr2 = ' edge '
print(mystr.rstrip())
# Delete the space to the left of the string
mystr3 = ' Left edge '
print(mystr.strip())\
# Fill character
q = 'hello'
print(q.ljust(10,'1'))

 


2.5. Judge the beginning of the character  

Fill in the following to judge whether the strings are the same

# Judge whether the beginning is
print(q.startswith('h',0,1))
# Judge whether it is all letters
a = ' good 121'
print(a.isalpha())
# Judge whether it contains only numbers
print(a.isdigit())
# Judging a string Are there any letters and Numbers
print(a.isalnum())
# Judge whether it's a space
kong = ' '
print(kong.isspace())


 2.6 Division and Splicing

  Splicing is a list All of them are spliced in a format you specify

# Merge and splice a character or original string
list1 = ['1','2','3','4','5','6']
new_Str = '...'.join(list1)
print(new_Str)

 

  Segmentation is to divide the characters you specify according to the characters you specify The first one running above

# toggle case Capitalize the first letter
strZhuan = 'qw eqwe qwe'
# Division Divide and return according to the characters you enter list
list = strZhuan.split("q")
print(list)

   3、 ... and .List

there list Operation and java equally

index Query subscript

# list
name_list = ['lanlan','zhangyue','wangba']
print(name_list[0])
print(name_list[1])
print(name_list[2])
# Check the subscript of the data you want
print(name_list.index('zhangyue',0,2))
# Record the data you specified in lis There are several times in it. If there is no error, report it
print(name_list.count('lanlan'))
# list Total length
print(len(name_list))
# Judge whether the data is list Inside
print('lanlan'in name_list)
print('lanlan'not in name_list)

  Delete list, Loop traversal list

# Case query whether the name entered by the user is in the set
name_listto = [' Zhang San ',' Li Si ',' Wang Wu ']
# Add data
name_listto.append('zhanghong')
# Add data Disassemble and add according to the subscript
name_listto.extend('123')
# Specify the subscript position to add data
name_listto.insert(0,'suijie')
# Remove elements
del_name = name_listto.pop(1)
del del_name
# Delete specified data
name_listto.remove(' Li Si ')
# Delete all
name_listto.clear()
print(name_listto)
name = input(' Please enter the name you want to search ')
if name in name_listto:
print(f'{name}, stay ')
else:
print(f'{name}, be not in ')
# modify
lsits = [1,2,3,4,5]
# Specify modification
lsits[0] = 10
print(lsits)
# Inverse data
lsits.reverse()
print(lsits)
# Sort true Descending
lsits.sort(key=None,reverse=False)
print(lsits)
# Copy list
copys = lsits.copy()
print(copys)
# Loop through the list
i = 0
while i <len(copys):
print(copys[i])
i+=1
for i in copys:
print(i)
# List nesting
qiantao_list = [['1,2,3,4'],['sui','asda']]
# Take the data
print(qiantao_list[1][1])

Four . Tuples  

Tuple data cannot be deleted

# Define tuple pairs
dingyi = (9,1,3,1,1,1,1)
# A single tuple must be added , Otherwise it would be int type
dingyi2 = (9,)
# Tuple query Subscript
print(dingyi.index(3))
# Number of queries
print(dingyi.count(1))
# Total number of tuples
print(len(dingyi))
# Tuple data modification
update_list = ("asd","asd",[1,2])
update_list[2][1] = 'asdad'
print(update_list[2][1])

                                                          ----------- The end ------------ 

 

 

 


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