浅谈python新手中常见的疑惑及解答
1 lambda函数
函数格式是lambda keys:express 匿名函数lambda是一个表达式函数,接受keys参数,返回表达式的值。所以不用return,也没有函数名,经常用在需要key参数的函数中,比如sorted。
2 元组(),它是以逗号辨别的,而不是小括号。比如一个元素的元组新手经常写成(12),其实他会被解释成单个元素12.正确的写法应该是(12,),在元素后面加上逗号。
3 模块导入。比如
import random
print random.choice(range(10))
和
from random import choice
print choice(range(10))
新手会有一种误解,第二种方法只导入了一个函数,而没有把整个模块导入,这是错误的。整个模块其实已经被导入,只是那个函数的引用被保存了起来。所以from-import这种语法不会带来性能上的差异,也没有节省内存。
4 当你有许多module,比如几百个,想要使用时可能会想一个一个导入太麻烦,有没有简便的方法?答案是有的,就是将这些模块组织成一个package。其实就是将模块都放在一个目录里,然后再加一个__init__.py文件,python会将其看作为package,使用里面的函数就可以以dotted-attribute方式来访问。
5 参数传递可变对象是传引用的,不可变对象是传值的。那么什么对象是可变的,什么是不可变的。所有python对象都有三个属性:类型、标识符和值,如果值是可变的就是可变对象,如果值不可变就是不可变对象。像数字、字符串、元组都是不可变对象,剩下的列表、字典、类、类实例等都是可变对象。
6 迭代器的理解,是实现了迭代器协议的容器对象。自己实现一个迭代器,类中要有__iter__()方法,该方法返回一个对象。这个对象要有__next__()方法,在next方法中的适当位置返回StopIteration异常。迭代器不经常使用,所以不用太担心。有替代方法就是生成器。
class MyIterator(object): """docstring for MyIterator""" def __init__(self, num): self.num = num def __iter__(self): return self; def __next__(self): if self.num <= 0: raise StopIteration; self.num -= 1; return self.num; for each in MyIterator(5): print(each); -> 结果
7 生成器。函数中只要出现了yield语句就会将其转变成一个生成器。在遇见yield语句后会保存上下文环境,并退出函数。
注意:生成器中没有return语句。
def fun2(num): print("start generator"); while(num>0): yield num; num -=1; a=[each for each in fun2(5)] print(a);->结果 start generator [5, 4, 3, 2, 1]
学习过程中,难免出错。如果您在阅读过程中遇到不太明白,或者有疑问。
以上这篇浅谈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.

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.

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.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

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.
