top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: What all rules are defined for variables in python ?

+3 votes
242 views

Sometimes python shell throws error while declaring variables . Is there any single place where all the rules are defined ?

posted Apr 23, 2016 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

A variable is a memory location where a programmer can store a value. The value stored in a variable can be accessed or updated later. The interpreter allocates memory on the basis of data type of a variable.

Variables names must start with a letter or an underscore, such as:

_underscore
underscore_

The remainder of your variable name may consist of letters, numbers and underscores.

password1
n00b
un_der_scores

Names are case sensitive.

case_sensitive, CASE_SENSITIVE, and Case_Sensitive are each a different variable.

Good Variable Name

Choose meaningful name instead of short name. roll_no is better than rn.
Maintain the length of a variable name. Roll_no_of_a-student is too long?
Be consistent; roll_no or or RollNo
Begin a variable name with underscore(_) character for special case.

answer Apr 27, 2016 by Manikandan J
Similar Questions
0 votes

I want to submit a qsub job to my hpc cluster from within python. In this case, I must set some environment variables specific for this qsub job and then invoking a bash script from within python.

What python code should be used for this job?

+2 votes

I want to take the file, "desktop/test.txt" and write it to "desktop/newfolder/test.txt". I tried the below script, and it gave me: "IOError: [Errno 2] No such file or directory: 'desktop/%s.txt'". Any suggestions would be great.

def firstdev(file):
 in_file = open("desktop/%s.txt") % file
 indata = in_file.read()
 out_file = open("desktop/newfolder/%s.txt", 'w') % file
 out_file.write(indata)
 out_file.close()
 in_file.close()

firstdev("test")
...