0% found this document useful (0 votes)
4 views

6.1. Python - Practice - Variables

The document provides practice exercises on Python variables, including questions about case sensitivity and identifier length. It includes multiple programs demonstrating valid and invalid variable names, along with expected outputs and errors for incorrect assignments. Key points highlight the rules for naming variables and common syntax errors encountered in Python.

Uploaded by

forunaveenadds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

6.1. Python - Practice - Variables

The document provides practice exercises on Python variables, including questions about case sensitivity and identifier length. It includes multiple programs demonstrating valid and invalid variable names, along with expected outputs and errors for incorrect assignments. Key points highlight the rules for naming variables and common syntax errors encountered in Python.

Uploaded by

forunaveenadds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Science – Python Variables Practice

Question 1 Is Python case sensitive when dealing with identifiers?

Answer Yes

Question 2 What is the maximum possible length of an identifier?

Answer There is no length limit for identifier

Program Printing name, id, salary


Name demo1.py
name = "Daniel"
emp_id = 11
emp_salary = 1000.123

print(name)
print(emp_id)
print(emp_salary)

Output

1|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Program Printing name, id, salary with meaningful info


Name demo2.py
name = "Daniel"
emp_id = 11
emp_salary = 1000.123

print("Emp name is: ",name)


print("Emp id is: ",emp_id)
print("Emp salary is: ",emp_salary)

Output

Program below variable names are valid or invalid


Name demo3.py
a=1
_a = 2 # single underscore
__a = 3 # two underscore
__a__ = 4 # two underscores starting and ending of the
variable

print(a)
print(_a)
print(__a)
print(__a__)

Output

2|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Program below variable names are valid or invalid


Name demo4.py

name = "Daniel"
name_1='Abhishek'
_ = 'Ramesh'

print(name)
print(name_1)
print(_)

Output

Program below variable names are valid or invalid


Name demo5.py
name = "Daniel"
name_1 = "Naresh"
1_name = "Srihari"
_ = "Ramesh"

print(name)
print(name_1)
print(1_name)
print(_)

Output

3|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Program below variable names are valid or invalid


Name demo6.py
name = "Daniel"
name_1 = "Abhishek"
$_name = "Srihari"
_ = "Ramesh"

print(name)
print(name_1)
print(1_name)
print(_)

Output

Program below variable name is valid or invalid


Name demo7.py
abc = 1,000,000
print(abc)

Output

4|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Program below variable name is valid or invalid


Name demo8.py
abc = 1,000,000
print(abc)
print(type(abc))

Output

Program below variable names are valid or invalid


Name demo9.py

a b c = 1000 2000 3000


print(a b c)

Output

Program below variable names are valid or invalid


Name demo10.py

a b c = 1000 2000 3000


print(a b c)
print(type(a b c))

Output

5|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Program below variable names are valid or invalid


Name demo11.py

a, b, c = 1000, 2000, 3000


print(a, b, c)

Output

Program below variable names are valid or invalid


Name demo12.py

a, b, c = 1000, 2000, 3000


print(a, b, c)
print(type(a, b, c))

Output

6|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Program below variable names are valid or invalid


Name demo13.py

a, b, c = 1000, 2000, 3000


print(a, b, c)

print(type(a))
print(type(b))
print(type(c))

Output

Program below variable name is valid or invalid


Name demo14.py
a_b_c = 1,2,3
print(a_b_c)

Output

7|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Program below variable name is valid or invalid

Name demo15.py
a_b_c = 1,2,3
print(a_b_c)

print(type(a_b_c))

Output

Program below program valid or invalid


Name demo16.py
10
print("hello")

Output

Program below program valid or invalid


Name demo17.py
a
print("hello")

Output

8|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

Invalid cases for variables

1. While creating a variable,

o Variable name should be written in left hand side.


o Value should be written in right hand side
o Otherwise we will get syntax error.

Program Creating variable in wrong direction


Name demo18.py

10 = emp_id
print(emp_id)

Error
SyntaxError: can't assign to literal

2. variables names,

o should not give as keywords names, otherwise we will get error

Program keywords we should not use as variable names


Name demo19.py

if = 10
print(if)

Error
SyntaxError: invalid syntax

9|Page 6.1. PYTHON VARIABLE PRACTICE


Data Science – Python Variables Practice

3. Variables name without value is invalid.

Program Variable without value is invalid.


Name demo20.py

a
print(a)

Error
NameError: name 'a' is not defined

10 | P a g e 6.1. PYTHON VARIABLE PRACTICE

You might also like