search
HomeBackend DevelopmentPython TutorialExample sharing on variable assignment operations in Python

Example sharing on variable assignment operations in Python

Jul 24, 2017 pm 03:12 PM
pythonoperateAssignment

This article mainly introduces the variable assignment operation of Python programming, and analyzes the usage skills of Python assignment, displacement, multiple assignment operators and other related operations in the form of examples. Friends in need can refer to the examples of this article

Describes the variable assignment operation in Python programming. Share it with everyone for your reference, the details are as follows:


#coding=utf8
'''''
Python中主要通过等号(=)进行赋值。
Python中的赋值不是直接将一个值赋给一个变量,
而是将该对象的引用(并不是值)赋值给变量。
'''
#赋值运算符
Int=12
Float=12.2
String="hello"
List=[1,2,"hell"]
Touple=(4,"hell")
Dictionary={'one':1,
      'two':2,
      }
'''''python的赋值语句不会返回值。'''
#add=(Int=Int+2) #错误的赋值语句
add=Int=Int+2 #python支持链式赋值
print add,Int
'''''增量赋值:等号和一个运算符组合一起并将计算结果重新赋值给左边的变量。'''
Int+=10
print "The Int+10=",Int
Float-=0.2
print "The Float-0.2=",Float
Int*=5
print "The Int *5=",Int
Int/=5
print "The Int/5=",Int
Int%=5
print "The Int%2=",Int
Int **=2
print "The Int **=",Int
Int<<=2#左移两位
print "The Int <<2=",Int
Int>>=2#右移两位
print "The Int>>2=",Int
Int &=10#按位相与
print "The Int &10=",Int
Int ^=3#按位取反
print "The Int^3=",Int
Int |=3#按位相或
print "The Int|3=",Int
#List加法
List+=[&#39;EWANG&#39;]
print "The List:",List
#多重赋值
a=b=c=d=e=f=8
print a,b,c,d,e,f
&#39;&#39;&#39;&#39;&#39;多元赋值:将多个变量同时赋值.
采用这种方式赋值时,等号两边的对象都是元组.
通常元组需要用圆括号()括起来.
圆括号是可选的,为了代码的可读性,建议加上圆括号
&#39;&#39;&#39;
x,y,z=4,8,"ewang" #为了代码可读性,建议使用圆括号
print x,y,z
(x,y,z)=(4,8,"ewang" )
print x,y,z
#Python的多元赋值方式可以实现无需中间变量交换两个变量的值
(x,y)=(y,x)
print x,y

The above is the detailed content of Example sharing on variable assignment operations in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How are arrays used in scientific computing with Python?How are arrays used in scientific computing with Python?Apr 25, 2025 am 12:28 AM

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

How do you handle different Python versions on the same system?How do you handle different Python versions on the same system?Apr 25, 2025 am 12:24 AM

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

What are some advantages of using NumPy arrays over standard Python arrays?What are some advantages of using NumPy arrays over standard Python arrays?Apr 25, 2025 am 12:21 AM

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making

How does the homogenous nature of arrays affect performance?How does the homogenous nature of arrays affect performance?Apr 25, 2025 am 12:13 AM

The impact of homogeneity of arrays on performance is dual: 1) Homogeneity allows the compiler to optimize memory access and improve performance; 2) but limits type diversity, which may lead to inefficiency. In short, choosing the right data structure is crucial.

What are some best practices for writing executable Python scripts?What are some best practices for writing executable Python scripts?Apr 25, 2025 am 12:11 AM

TocraftexecutablePythonscripts,followthesebestpractices:1)Addashebangline(#!/usr/bin/envpython3)tomakethescriptexecutable.2)Setpermissionswithchmod xyour_script.py.3)Organizewithacleardocstringanduseifname=="__main__":formainfunctionality.4

How do NumPy arrays differ from the arrays created using the array module?How do NumPy arrays differ from the arrays created using the array module?Apr 24, 2025 pm 03:53 PM

NumPyarraysarebetterfornumericaloperationsandmulti-dimensionaldata,whilethearraymoduleissuitableforbasic,memory-efficientarrays.1)NumPyexcelsinperformanceandfunctionalityforlargedatasetsandcomplexoperations.2)Thearraymoduleismorememory-efficientandfa

How does the use of NumPy arrays compare to using the array module arrays in Python?How does the use of NumPy arrays compare to using the array module arrays in Python?Apr 24, 2025 pm 03:49 PM

NumPyarraysarebetterforheavynumericalcomputing,whilethearraymoduleismoresuitableformemory-constrainedprojectswithsimpledatatypes.1)NumPyarraysofferversatilityandperformanceforlargedatasetsandcomplexoperations.2)Thearraymoduleislightweightandmemory-ef

How does the ctypes module relate to arrays in Python?How does the ctypes module relate to arrays in Python?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingandmanipulatingC-stylearraysinPython.1)UsectypestointerfacewithClibrariesforperformance.2)CreateC-stylearraysfornumericalcomputations.3)PassarraystoCfunctionsforefficientoperations.However,becautiousofmemorymanagement,performanceo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor