TOPIC: Python Structure and Variables
Python structure
Python has a very simple structure and it is easy to use. No semi-colons to generate any kind of
syntax errors also. The most basic syntax is the print() statement.
print() is a built-in Python function used to output text or variables to the console.
"Hello, World!" is the text we want to print, enclosed in double quotation marks.
Variable
A variable is a named storage location used to store data that can change during the execution of a
program.
Variable example:
variable_name = 4
Types of naming a variable:
Camel Case example
myVariableName = 4
Camel Case example
MyVariableName = 4
Camel Case example
my_variable_name = 4
We can also define the variable as
Other ways to define a variable
Multiple Values to multiple variables
x, y, z = "Red", "Blue", "Green"
One Value to multiple variables
x = y= z = "Red"
Unpack a collection
color = ["red", "green", "blue"]
x = y = z = color
Output Variable
The python print() function is often used to output variables.
Let us look at some of the output structure
x = “This is python”
y = "Programming"
z = "reading at RJ's Blog"
a = 5
b = 6
print(x)
print(x, y, z)
print(a + b)
Output:
This is python
This is python programming reading at RJ's Blog
11
Global variable
Variables that are created outside of a function are known as global variables.
Example
x = "Python"
def sample():
print(“This is” + x)
sample()
Local variable
If we create a variable with the same name inside a function, this variable will be local, and can
only be used inside the function
Example
x = “Global”
def sample():
x = “Local”
print(“Python” + x)
Global keyword
Normally when we create a variable inside a function that variable is local and can only be used
inside that function
Example
def sample():
global x
x = “interesting”
sample()
print(“Python is ” + x)
Code as discussed in the video:
# Variables in Python
# variable = 5
# print(variable)
# 1 - camel case
# myVarName = "Hello"
# 2 - pascal case
# MyNewVar = "Python Programming"
# 3 - snake case
# my_new_Var = "How are you"
# print(myVarName, MyNewVar, my_new_Var)
# x, y, z = "Red", "Green", "Blue"
# print(x, y, z)
# x = "Red", "Green", "Blue"
# print(x)
# x = y = z = 56
# print(x, y, z)
# Global variable
# glo = 56
# print("The global variable is ", glo)
# def sample():
# # Local variable
# glo = "turned into 2"
# print("The global is", glo)
# sample()
# print("The global variable is ", glo)
hellovar = 56
print(f"The value of hellovar is {hellovar}") # fstring
You can copy and run this code