Python Variables

Python Variables

In python variables are the reserved memory locations used for storing values. In python a built-in id() function returns the address where the variable is stored.

For example:

week = "Week 30"
print(id(week))        

Python has no command for declaring a variable. A Python variable is created automatically when you assign a value to it. The equal sign (=) is used to assign values to variables.

The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable.

age = 52 # Creates an integer variable
distance = 16.2 # Creates a floating point variable
name = "Ttalib" # Creates a string variable

print(age)
print(distance)
print(name)        

Casting

If you want to specify the data type of a variable, this can be done with casting.

x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0        

Get the Type

You can get the data type of a variable with the in-built type() function.

x = 5
y = "John"

print(type(x)) # <class 'int'>
print(type(y)) # <class 'str'>        

Single or Double Quotes?

String variables can be declared either by using single or double quotes:

x = "John"

# is the same as

x = 'John'

print(x)        

Case-Sensitive

Variable names are case-sensitive.

a = 4

A = "Sally"

#A will not overwrite a

print(a)

print(A)        

How to Name Your Variable

The following rules must be followed assigning names to variables

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • A variable name cannot be any of the Python keywords.

The following variable names are valid:

thevar = "Aisha"

the_var = "Aisha"

thevar = "Aisha"

THEVAR = "Aisha"

thevar2 = "Aisha"        

Techniques For Multi-Words Variable Names

  • Camel Case: Each word, except the first, starts with a capital letter:

theVariableName = "Halima"        

  • Pascal Case: Each word starts with a capital letter:

TheVariableName = "Muhammed"        

  • Snake Case: Each word is separated by an underscore character:

the_variable_name = "John"        

Assigning Multiple Values To variables

  • Assign Many Values to Multiple Variables

x, y, z = "Rice", "Maize", "Paddy Rice"

print(x)

print(y)

print(z)        

  • Assign One Value to Multiple Variables

x = y = z = "Paddy Rice"

print(x)

print(y)

print(z)        

If you have a collection of values in a list. Python allows you to extract the values into variables. This is called unpacking.

commodity = ["Rice", "Maize", "Paddy Rice", "Cocoa"]

w, x, y, z = commodity

print(w)

print(x)

print(y)

print(z)        

Global & Local Variables

Variables that are created outside of a function are global variable while those created inside of function are local variables (unless is created using the global keyword). Global variables can be used by everyone, both inside of functions and outside while local variables can only be used inside the function.

x = "Abdulmutalib"

def nameFunc():
  print("My name is " + x)

nameFunc()        

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

x = "Abdulmutalib"

def nameFunc():
  x = "Muhammed"
  print("My name is " + x)

nameFunc()

print("My name is " + x)        

The global Keyword

To create a global variable inside a function, you can use the global keyword.

def nameFunc():
  global x
  x = "Abdulmutalib"

nameFunc()

print("My name is " + x)        

Also, use the global keyword if you want to change a global variable inside a function.

x = "Abdulmutalib"

def myfunc():
  global x
  x = "Muhammed"

myfunc()

print("My name is " + x)        

要查看或添加评论,请登录

Abdulmutalib Idris的更多文章

  • Improving Nigerian Education Through School Management Systems

    Improving Nigerian Education Through School Management Systems

    The Nigerian education sector faces numerous challenges, particularly at the higher institution level. These include…

  • Python Lambda

    Python Lambda

    Is a function that take as many arguments as possible but can only have one expression. They are also consider…

  • Python Functions

    Python Functions

    A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a…

    2 条评论
  • Python Conditional and Loop Statements

    Python Conditional and Loop Statements

    If ..

    1 条评论
  • Python Datatype - Part 5 (Dictionary)

    Python Datatype - Part 5 (Dictionary)

    As we learnt earlier Dictionary is one of 4 built-in data types in Python used to store collections of data, the other…

    2 条评论
  • Python Datatype - Part 4 (Sets)

    Python Datatype - Part 4 (Sets)

    Python Sets As we learnt earlier Set is one of 4 built-in data types in Python used to store collections of data, the…

    1 条评论
  • Python Datatype - Part 3 (Tuples)

    Python Datatype - Part 3 (Tuples)

    Python Tuples As we learnt in the last class there are datatype use to store multiple items in single variable. Python…

  • Python Datatype - Part 2 (Lists)

    Python Datatype - Part 2 (Lists)

    Python Lists There are datatype use to store multiple items in single variable. Python have 4 built-in datatype that…

    1 条评论
  • Python Datatype - Part 1

    Python Datatype - Part 1

    In the last class we talked about variables and we said variable data type is whatever values are assigned to the…

  • Python Indentation, Multi-line Statements, and Quotations

    Python Indentation, Multi-line Statements, and Quotations

    Python Indentation The spaces left at the beginning of a code line is called indentation. In other languages indenting…

    1 条评论

社区洞察

其他会员也浏览了