Basic (3) - basic data type

Foundation of Python ﹣ fullstack (3)

1. Int (numeric type)

1.1 INTRODUCTION

Represents the number 0 - 9

1.2 common methods

Bit length ()

# Because the binary of decimal number 18 is 10010, i.e. bin(18) = 10010, the bit length of 18 is 5

i = 18
print(i.bit_length())

>>>5

2. Boolen (boolean type)

2.1 introduction

Boolean values only have True and False, and True corresponds to 1 in binary, and False corresponds to 0 in binary

#Conversion between other data types and Boolean types
int -> boolen
    Boolean value of non-0 value: True
    Boolean value of number 0: False

str-> boolen
    Boolean value of non empty string: True
    Boolean value of empty string: False

boolen->int
    True: 1
    False: 0

3. String (string type)

3.1 introduction

In single quotation mark, double quotation mark and triple quotation mark, a string is composed of a string of characters

3.2 index and slice of string

String index

s = 'ABCDEFGHIJK'
print(s[0])
>>> 'A'

print(s[2])
>>> 'C'

print(s[-1])
>>>'K'

print(s[-2])
>>> 'J'

String slicing: looking at the head and ignoring the tail

s = 'ABCDEFGHIJK'
print(s[0:4])
>>>'ABCD'

print(s[0:-1])
>>>'ABCDEFGHIJ'

print(s[:])
>>>'ABCDEFGHIJK'

print(s[0:])
>>>'ABCDEFGHIJK'

print(s[0:0])
>>>

s = 'ABCDEFGHIJK'  # s [head: Tail: step size]
print(s[0:5:2])
>>>'ACE'

print(s[4:0:-1])
>>>'EDCB'

print(s[3::-1])
>>>'DCBA'

print(s[3::-2])
>>>'DB'

print(s[-1::-1])
>>>'KJIHGFEDCBA'

print(s[::-1])
>>>'KJIHGFEDCBA'

3.3 common methods

s = 'PytHOn'

# captalize,swapcase,title
print(s.capitalize())
>>>'Python'

print(s.swapcase())
>>>'pYThoN'

s1 = '-python*fullstack@s9'
print(s1.title())
>>>'-Python*Fullstack@S9'

# upper,lower
print(s.upper())
>>>'PYTHON'

print(s.lower())
>>>'python'

# center,expandtabs
print(s.center(20, '*'))
>>>'*******PytHOn*******'

# Not all 8 or multiple spaces before 8, if any
s2 = 'python\tfullstack'
print(s2.expandtabs())
>>>'python  fullstack'

# Public method len()
print(len(s))
>>>6

# startswith,endswith
print(s.startswith('Py'))
>>>True

print(s.startswith('Py', 1, 4))
>>>False

print(s.endswith('On'))
>>>True

print(s.endswith('On'), 4)
>>>True

# find,index
print(s.find('H'))
>>>3

print(s.index('H'))
>>>3

# strip rstrip lstrip
s3 = '  python '
print(s3.strip())
>>>'python'

s4 = ' @python*@'
print(s4.strip('*@'))
>>>' @python'

print(s.rstrip())
>>>' @python'

print(s.lstrip())
>>>' @python*@'

# count
s5 = 'life is short we use python'
print(s5.count('s'))
>>>3

s6 = 'lifeisshortweusepython'
print(s6.split('s'))
>>>['lifei','','hortweu','epython']

# Three uses of format
s7 = 'Hello, everyone. My name is{},This year{},I like it{},Again, my name is{}'
print(s7.format('ELijah', 26, 'coding', 'Elijah'))
>>>Hello, everyone. My name is ELijah,This year26,I like it coding,Again, my name is Elijah

s8 = 'Hello, everyone. My name is{0},This year{1},I like it{2},Again, my name is{0}'
print(s8.format('ELijah', 26, 'coding'))
>>>Hello, everyone. My name is ELijah,This year26,I like it coding,Again, my name is Elijah

s9 = 'Hello, everyone. My name is{name},This year{age},I like it{hobby},Again, my name is{name}'
print(s9.format(hobby='coding', name='ELijah', age=26))
>>>Hello, everyone. My name is ELijah,This year26,I like it coding,Again, my name is Elijah

Tags: Python

Posted on Thu, 07 May 2020 10:53:54 -0400 by ~n[EO]n~