


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of variables in python, including their declaration, naming rules, and usage. Topics covered include data types, variable declaration, concatenation, re-declaration, and deletion. The document also explains assignment operators and the difference between lvalues and rvalues.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
What is a Variable in Python? A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing. Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables can be declared by any name or even alphabets like a, aa, abc, etc Variable Naming Rules in Python
How to Declare and use a Variable Let see an example. We will declare variable "a" and print it. a= print (a) Re-declare a Variable You can re-declare the variable even after you have declared it once. a= print(a) a=’AECS Jaduguda’ print(a) Concatenate Variables a='AECS' b= print(a+b) will throw error , as we cannot concatenate two different datatypes. But a='AECS' b= print(a+str(b)) will display AECS Delete a variable You can also delete variable using the command del "variable name". The below table displays the list of available assignment operators in Python language.
(e) These complex assignments happen in parallel, so we can exchange values with:
a,b = b,a Really: it's tuple assignment! (f) Here's Euler's algorithm for finding greatest common divisors: def gcd(a,b): while a > 0: a,b = (b,a) if a > b else (b%a,a) return b (g) when an asterisk precedes a variable name used as an l-value, it means assign this variable the remaining r-values as a list. This is very powerful: car,*cdr = (1,2,3) cdr [2, 3] Summary Variables are referred to "envelop" or "buckets" where information can be maintained and referenced. Like any other programming language Python also uses a variable to store the information. Variables can be declared by any name or even alphabets like a, aa, abc, etc. Variables can be re-declared even after you have declared them for once In Python you cannot concatenate string with number directly, you need to declare them as a separate variable, and after that, you can concatenate number with string Declare local variable when you want to use it for current function Declare Global variable when you want to use the same variable for rest of the program To delete a variable, it uses keyword "del".