python3 基本数据类型 Python3 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。 Python3 中有6个标准的数据类型:Number(数字);字符串(String);列表(list);元组(Tuple);字典:(Dict);集合(Sets) Number(数字): Python3支持int,float,bool,complex(复数) type()函数可以查看变量所指的对象类型 String(字符串): Python中的字符串用单引号(')或双引号(")括起来,同时使用反斜杠(\)转义特殊字符。 注意:'''...'''三元引号在创建短字符串时没有什么特别用处,主要用于创建多行字符串,如下例: >>> poem = '''There was a Young Lady of Norway, ... Who casually sat in a doorway; ... When the door squeezed her flat, ... She exclaimed, "What of that?" ... This courageous Young Lady of Norway.''' Python也允许空串存在,不包含任何字符,完全合法! 数字与字符串之间的转换: 字符串转换成数字: >>> int('20') 20 >>> float('20') 20.0 >>> int(20) 20 数字转换成字符串: >>> str(20) '20' >>> str(20.0) '20.0' >>> str(True) 'True' 使用+拼接 在python中可以试用+将多个字符串或字符串变量进行拼接 >>> 'Release the Tom!' + 'At once!' Release the Tom! At once! 使用[]提取字符 在字符串后面添加[],在[]中添加偏移量,即可取出该位置的字符串。第一个字符偏移量为0,第二个为1,后面依次类推。 右边第一个偏移量为-1,第二个为-2,从右往左依次类推... >>> str = 'abcdefghijklmnopqrstuvwxyz' >>> str[0] 'a' >>> str[-1] 'z' >>> str[3] 'd' 字符串是不可变的,有时候改变字符串,需要组合使用一些字符串函数:replace(),以及分片操作 >>> name = 'Henny' >>> name.replace('H','P') 'Penny' >>> 'P' + name[1:] 'Penny' 使用[start:end:step]分片: 分片操作:可以从一个字符串中抽取子字符串。起始偏移量start,终止偏移量end以及可选的步长step来定义一个分片 1.[:]提取从开头到结尾的整个字符串 2.[start:]从start提取到结尾 3.[:end]从开头提取到end-1 4.[start:end]从start提取到end-1结尾 5.[start:end:step]从start提取到end-1,每个step提取一个字符 >>> str = 'qwertyuiop' >>> str[:] 'qwertyuiop' >>> str[5:] 'yuiop' >>> str[-3:] 'iop' >>> str[:-3] 'qwertyu' >>> str[-6:-3] 'tyu' 步长为3 >>> str[::3] 'qrup' 利用切片反向输出字符串 >>> str[::-1] 'poiuytrewq' 字符串其他常用操作: >>> str = 'qwertyuiop' 计算字符串的长度 >>> len(str) 10 使用split()分割: 使用内置的字符串函数 split() 可以基于 分隔符 将字符串分割成由若干子串组成的 列表 。 所谓列表(list)是由一系列值组成的序列,值与值之间由逗号隔开,整个列表被方括号所包裹。 >>> todos = 'get gloves,get mask,give cat vitamins,call ambulance' >>> todos.split(',') ['get gloves', 'get mask', 'give cat vitamins', 'call ambulance'] 上面例子中,字符串名为 todos,函数名为 split(),传入的参数为单一的分隔符split(),传入的参数为单一的分隔符 ','。 如果不指定分隔符,那么 split() 将默认使用空白字符——换行符、空格、制表符。 >>> todos.split() ['get', 'gloves,get', 'mask,give', 'cat', 'vitamins,call', 'ambulance'] 使用join()合并: 可能你已经猜到了,join() 函数与 split() 函数正好相反:它将包含若干子串的列表分解,并将这些子串合成一个完整的大的字符串。join() >>> crypto_list = ['Yeti', 'Bigfoot', 'Loch Ness Monster'] >>> crypto_string = ', '.join(crypto_list) >>> print('Found and signing book deals:', crypto_string) Found and signing book deals: Yeti, Bigfoot, Loch Ness Monster 使用replace()替换: >>> str = 'qwertyuiop' >>> str.replace('w','ooooo') 'qoooooertyuiop' 最多修改3处 >>> str = 'qwerwerwerwtytewwiitw' >>> str.replace('w','oooo',3) 'qooooerooooerooooerwtytewwiitw' 计算字符串中'w'出现的次数 >>> str.count('w') 7
The above is the detailed content of Introduction to Python basic data types. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools