Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Variables in Python: Declaration, Rules, and Usage, Study notes of Advanced Computer Programming

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

2021/2022

Uploaded on 08/05/2022

nguyen_99
nguyen_99 🇻🇳

4.2

(80)

1K documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
1. Variable name should start with letter(a-zA-Z) or underscore (_).
Valid : age , _age , Age
Invalid : 1age
2.In variable name, no special characters allowed other than underscore (_).
Valid : age_ , _age
Invalid : age_*
3.Variables are case sensitive.
age and Age are different, since variable names are case sensitive.
4.Variable name can have numbers but not at the beginning.
Example: Age1
5.Variable name should not be a Python keyword.Keywords are also called as reserved
words.
Example
pass, break, continue.. etc are reserved for special meaning in Python. So, we should
not declare keyword as a variable name.
pf3
pf4

Partial preview of the text

Download Understanding Variables in Python: Declaration, Rules, and Usage and more Study notes Advanced Computer Programming in PDF only on Docsity!

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

  1. Variable name should start with letter(a-zA-Z) or underscore (). Valid : age , age , Age Invalid : 1age 2.In variable name, no special characters allowed other than underscore (). Valid : age , age Invalid : age* 3.Variables are case sensitive. age and Age are different, since variable names are case sensitive. 4.Variable name can have numbers but not at the beginning. Example: Age 5.Variable name should not be a Python keyword.Keywords are also called as reserved words. Example pass, break, continue.. etc are reserved for special meaning in Python. So, we should not declare keyword as a variable name.

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".