Chapter 5 - Python Variables



AD

5.1.0 Variables

Variable names in Python can be any length and can consist of uppercase and lowercase letters ( A-Z , a-z ), digits ( 0-9 ), and the underscore character ( _ ). Although a variable name can contain digits, the first character of a variable name cannot be a digit.

Variable names are case sensitive, and variable names are lowercase by convention. This means that My_variable and my_variable are different variable names.

A variable is created when you assign it a value. This is called declaring a variable. The interpreter infers the type of variable by how it is assigned. (Notice the comments which begin with the # sign. They are not interpreted.)

name = 'Steve'        #name is a string
age = 45        #age is an integer
number = 8.7        #number is float
print(type(name))       #prints the type as string
print(type(age))       #prints the type as integer
print(type(number))       #prints the type as float

We can tell how a variable is assigned by using the type() function.

Go to File→New→Python File and type in a name such as test-2. Hit Enter. Copy and paste the code above into your PyCharm code window and run it.

IMPORTANT: When running code the quote marks must be of the straight kind. If copy and pasting from the website to your computer you may have to retype the quote marks in PyCharm to make them straight or you will get an error message.

The results will be as shown below.

class 'str'
class 'int'
class 'float'



5.2.0 Python Variable Scope

Scope is the region in a program where a variable operates.

Variable created outside a function is available outside and inside all functions. It is called a global variable.
Variable created inside a function is available inside that function. The variable is called local in scope.
Variable created inside a function within a function is available to both functions.
The same variable assigned inside and outside has local scope in function.
If you want a variable inside a function to be available everywhere (globally) then you must label it as global.

#SCOPE
# Variable assigned outside a function has global scope
# Variable assigned inside a function has local scope
# Variable created inside a function within a function is available to both functions.
# The same variable assigned inside and outside has local scope in function
# A variable declared global within a function is global



# Global scope
myint = 5
def sumfunct():
   return myint * 2
print(sumfunct())    

# Output is 10


# Local scope
def sumfunct():
    myint = 4
    return myint
print(sumfunct())

# Output is 4


#Local scope for myint
myint = 5
def sumfunct():
   myint = 9
   return myint * 2
print(sumfunct())

# Output is 18


def sumfunct():
   x = 20
   def funcinfunc():
       print (x)     # x=20
   return funcinfunc()
sumfunct()    # Call sumfunct()

# Output is 20


#Global scope
myint = 5
def sumfunct():
    myint = 8
    return myint     # myint = 8
print(sumfunct())

# Output is 8


def sumfunct():
   global x
   x = 6
   return x
sumfunct()
print (x)

# Output is 6


Engineering-Python

SALARSEN.COM
Table of Contents
Ch1-Install Python
Ch2-Install PyCharm
Ch3-Save Work
Ch4-Add Project
Ch5-Variables
Ch6-Print&Input
Ch7-Lists
Ch8-Loops
Ch9-If&Logical
Ch10-Functions
Ch11-Bubble Sort
Ch12-Plotting
Ch13-Files
Ch14-Print Format
Ch15-Dict&Comp&Zip
Ch16-Arrays
Ch17-Electrical
Ch18-Regression
Ch19-Differential
Ch20-Secant