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

What are the basic data types of Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the basic data types of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic data types of Python"?

1. Numerical value

Int

Flaot

two。 String String

Get length

Three ways of string concatenation

String slicing

Specify character lookup

Character count

3. Boolean boolean

True

Flase

4. Null None

5.list list

Elements can be of different types, ordered collections.

6. Tuple tuple

The element cannot be changed after being defined

7. Dictionary dictionary

Store the key value and find it very quickly.

8. Set sets

Disordered and unrepeated sequence

9. Use a Mini Program to illustrate the format specification of python.

One: numerical value

There are only two types of values in Python

1.int

2.float

Defining values is very convenient and reduces a lot of definitions, such as the definition of axi3 in c

Int a = 3 Spacer c

A = 3#python

Python will automatically identify your number as int or float, so it is slower than the c language.

A = 3.14#float

Constants are defined in uppercase, such as PI=3.14, and in other languages, constants are immutable. They can be changed in python due to loss of qualification, but try not to change them.

> PI=3.14

> PI

3.14

> PI = PI+2

> PI

5.140000000000001

**

Two: string

**

Definition of string

Direct assignment is used to define strings in python. Strings can be enclosed in single quotation marks, double quotation marks and three quotation marks.

> sworn roomabc'

> > s

'abc'

> s= "abcd"

> > s

'abcd'

> slotted girls, please abdcboys

> > s

'abdc'

> len (s) # len () get length

four

There are three ways to concatenate strings:

1JI + number splicing

> s = 'abc'+'bcd'

> > s

'abcbcd'

> S1 = sworn roomabc'

> > S1

'abcbcdabc'

2.list ordered set concatenation (using the + sign to concatenate a large number of characters requires a lot of code)

> list_str = ['ni','hao','bei','jin']

> str2 = '.join (list_str)

> str2

'nihaobeijin'

>

> str3

'ni hao bei jin'

3.format concatenation: use {} to empty the strings that need to be stitched, followed by stitching

> str3 ='I like {} because I need {} '.format (' python',' efficient')

> str3

"I like python because I need to be efficient."

The slicing of the string: (start: end: interval), do not write from the beginning and the end by default. You can also use split cutting:

> s = 'abcdefgh'

> > s [0:3]

> s [: 3] starts from 0 by default

'abc'

> > s [3:]

'defgh'

> > s [:-1]

'abcdefg'

> > s [:-2]

'abcdef'

> s = 'abcdegh'

> s [:: 2] / / start, end, interval

'aceh'

> a = 'abcdef'

> a [::-1] / / can be used for string inversion

'fedcba'

> str4 = "Xiao Hong, Xiao Bai, Xiao Hua"

Str4.split (",")

['Xiao Hong, Xiao Bai, Xiao Hua']

String lookup:

> s = 'abcdefgh'

> str2.find ('a')

0

# cannot find and return-1

> s = 'abc'

> s.find ('d')

-1

String subscript query:

> s = 'abcdefgh'

> str2.index ('b')

one

String count:

> str3 = 'aaabbbccc'

> str3.count ('b')

three

Three: Boolean

Contains only two values, True and False. (clearly case sensitive)

> > 3 > > 3 > 2

True

> 3 > 2 and 3 > > 3 > 2 or 3 > not 3 > false

False

Four: None value

Represents an empty type

> s = None

> > s

> type (s) # use type () to view parameter types

Five: list list

The elements in list are unrestricted # and are ordered collections, so you can get the

Classmates = ['yh','xiaobai','xiaohong']

> type (classmates)

> len (classmates)

three

> classmates [0]

'yh'

> classmates [- 1]

'xiaohong'

List added, directly added in the last bit

Classmates.append ('')

Classmates

['yh',' xiaobai', 'xiaohong',' ']

Insert, you can specify the location to insert

> classmates.insert

> classmates

['yh',' hehe', 'xiaobai',' xiaohong', '']

Pop-up element, the default is the last bit, or you can specify a location

> > classmates.pop ()

''

Classmates.pop (3)

'xiaohong'

> classmates

['yh',' hehe', 'xiaobai']

The element in list can be a list

> s = ['python','java', [123123],' hehe']

> > len (s)

four

Six: tuple

Once the definition of the # element cannot be changed, the reference can be modified.

> t = (1pm 2pm 3)

> > t

(1, 2, 3)

> t [1]

two

> t = ('1pm, 2pm, 3')

> > t

('1','2','3')

Elements cannot be modified after they are defined

> t = ('1pm, 2pm, 3')

> > t

('1','2','3')

> l = [1BI 2]

> > t [2] = l

Traceback (most recent call last):

File "", line 1, in

TypeError: 'tuple' object does not support item assignment

> > t [2] = 3

Traceback (most recent call last):

File "", line 1, in

TypeError: 'tuple' object does not support item assignment

But the reference can be modified, that is, it can be modified when the element in the tuple is a reference.

> l = [1pc2jp3] Zhengzhou abortion price http://www.zzzykdfk.com/

> t = (1pm 2pm l)

> l = [3, 2, 2, 1]

> > t

(1, 2, [1,2,3])

> t [2] [0] = 3ram / represents the first element in the third element l of t.

> > t

(1,2, [3,2,3])

Seven: dictionary

A collection of dictionnary unordered objects, mapped through key-value. Marked with "{}", the search speed is extremely fast, just find the key value. It takes up a lot of memory (storing not only values, but also key values). Key uses immutable objects and uses key to calculate the location, usually using the hash algorithm.

> names = ['yh','','hehe']

>

> d = {'yh':90,'xiaohong':20,'hehe':70}

> d ['hehe']

seventy

> d ['xiaogou'] = 3 words added, out of order.

> > d

{'yh': 90,' xiaohong': 20, 'hehe': 70,' xiaogou': 30}

Find out if it exists

> 'hehe' in d

True

> 123 in d

False

> d.get ('123century maxim 1) # if not found, return-1. Check a certain value without looking for it first, which will report an error. Searching first can prevent it from reporting an error.

-1

> d.pop ('xiaogou')

sixty

> > d

{'yh': 90,' xiaohong': 20, 'hehe': 70}

Eight: gather sets

A sequence of unordered, non-repeating elements, often used to remove duplicates, intersections, and unions. Very fast

> s = set ([1, 1, 3, 3, 4, 4])

> > s

{1, 3, 4}

> > s.add (3)

> > s

{1, 3, 4}

> s.add (2)

> > s

{1, 2, 3, 4}

> S1 = set ([1pm 2p3])

> S2 = set ([2pm 3pr 4])

S1 & S2

{2, 3}

> > S1 | S2

{1, 2, 3, 4}

Nine: hail conjecture

Arbitrarily write a positive integer N and transform it according to the following rules:

If it is an odd number, the next step becomes 3N+1.

If it is an even number, the next step is to change to NUnip 2.

Will eventually become 1.

Code:

Def collatz (number): # def is a function of python, usually starting with "def function name (argument):"

The if number%2 = = 0 if # function is formatted with four spaces, that is, a tab key, followed by the following:

The statement tab after return number//2#if

Elif number%2 = = 1:#elif is the same, that is

Return 3*number+1

Print ('Please inter a number:') # print enter an integer on the screen

Try:# error check. If the input is not a positive integer, an error will be reported.

Num = int (input ())

If the content is true, it will do this loop all the time. This number is not equal to 1, then it will always operate on this number.

Print (collatz (num))

Num = collatz (num)

Except ValueError:#, this is the error message.

Print ('you must input an int number')

Thank you for your reading, the above is the content of "what are the basic data types of Python". After the study of this article, I believe you have a deeper understanding of what the basic data types of Python have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 238

*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