python动态性强类型用法实例
本文实例分析了python动态性强类型用法。分享给大家供大家参考。具体如下:
Python变量声明和定义
与C#不同,Python在使用变量之前无须定义它的类型,试着运行下面的例子:
i = 12 print i
从上边我们可以看到,变量 i 在使用前并不需要定义,但是必须声明以及初始化该变量。试着运行下面的例子:
i = 1 print i + j
上面的代码会产生一个异常:“NameError: name 'j' is not defined”,Python提示变量 j 没有定义。这点和BASIC等弱类型的语言不一样。在BASIC中,执行上述代码的时候不会产生异常,你可以在EXCEL的VBA开发环境里试一下,把 print改为MsgBox就可以,结果会输出 1 。这说明Python并不是一种类似BASIC的弱类型语言。
另一方面,Python与C#有一个很大的差异就是在程序运行过程中,同一变量名可以(在不同阶段)代表不同类型的数据,看看下边的例子:
i = 1 print i,type(i),id(i) i = 10000000000 print i,type(i),id(i) i = 1.1 print i,type(i),id(i)
变量 i 的类型在程序执行过程中分别经历了int、long和float的变化,这和静态类型语言(如C等)有很大不同。静态语言只要一个变量获得了一个数据类 型,它就会一直是这个类型,变量名代表的是用来存放数据的内存位置。而Python中使用的变量名只是各种数据及对象的引用,用id()获取的才是存放数 据的内存位置,我们输入的1、10000000000和1.1三个数据均会保存在id()所指示的这些内存位置中,直到垃圾回收车把它拉走(在系统确定你 不再使用它的时候)。这是动态语言的典型特征,它确定一个变量的类型是在给它赋值的时候。
另一方面,Python又是强类型的,试着运行下边的例子:
# -*- coding: utf-8 -*- i = 10; j = 'ss' print i+j #正确的写法是print str(i)+j,输出10ss
会产生一个异常:“TypeError: unsupported operand type(s) for +: 'int' and 'str'”。在BASIC等弱类型的语言中,上边的例子会正常运行并返回(虽然有时候是不可预期的)结果。
所以,我们说Python既是一种动态类型语言,同时也是一种强类型的语言,这点是和C#不同的地方。对于Python的这种变量的声明、定义和使 用方式,C#程序员可能要花一段时间去适应,不过相信你会很快就喜欢上它,因为它让事情变得更加简单(而且不会不安全)。而且,C# 4.0 已经开始用类似的方式定义和使用变量(通过在变量名前加关键字dynamic),如果你先学了Python变量,将能够更快的适应C# 4.0的动态编程特征。
希望本文所述对大家的Python程序设计有所帮助。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
