python global variable
In Python, a global variable is a variable that is defined outside of a function and is accessible from within any function. Here's an example:
VerifyOpen In EditorEditCopy code1x = 5 # global variable
2
3def my_function():
4 print(x) # Output: 5
5
6my_function()
In this example, the variable x
is defined outside of the function my_function()
, making it a global variable. The function my_function()
can access and print the value of x
.
However, if you try to modify a global variable within a function, you need to use the global
keyword to indicate that the variable is global. For example:
VerifyOpen In EditorEditCopy code1x = 5 # global variable
2
3def my_function():
4 global x
5 x = 10 # modify the global variable
6 print(x) # Output: 10
7
8my_function()
9print(x) # Output: 10
In this example, the function my_function()
modifies the global variable x
using the global
keyword.
Note that if you don't use the global
keyword, Python will treat the variable as a local variable within the function, and any changes made to it will not affect the global variable. For example:
VerifyOpen In EditorEditCopy code1x = 5 # global variable
2
3def my_function():
4 x = 10 # local variable, does not affect the global variable
5 print(x) # Output: 10
6
7my_function()
8print(x) # Output: 5
python data types
In Python, there are several built-in data types that can be used to store and manipulate data. Here are the main data types in Python:
Numeric Types
Int : Integer, a whole number, e.g.
1
,2
,3
, etc.Float : Floating-point number, a number with a fractional part, e.g.
3.14
,-0.5
, etc.Complex : Complex number, a number with a real and imaginary part, e.g.
3+4j
,-2-3j
, etc.
Sequence Types
str
: String, a sequence of characters, e.g."hello"
,'hello'
, etc. Strings can be enclosed in single quotes or double quotes.list
: List, a collection of items that can be of any data type, e.g.[1, 2, 3]
,["a", "b", "c"]
, etc.tuple
: Tuple, a collection of items that can be of any data type, e.g.(1, 2, 3)
,("a", "b", "c")
, etc. Tuples are similar to lists, but they are immutable.
Mapping Type
Dict
: Dictionary, a collection of key-value pairs, e.g.{"name": "John", "age": 30}
, etc.
Set Types
set
: Set, an unordered collection of unique items, e.g.{1, 2, 3}
,{"a", "b", "c"}
, etc.Frozen-set
: Frozen set, an unordered collection of unique items that cannot be changed, e.g.frozenset({1, 2, 3})
, etc.
Boolean Type
bool
: Boolean, a value that can be eitherTrue
orFalse
.
Binary Types
Bytes
: Bytes, a sequence of integers in the range 0 <= x < 256, e.g.b"hello"
, etc.Byte-array
: Byte array, a mutable sequence of integers in the range 0 <= x < 256, e.g.bytearray(b"hello")
, etc.
None Type
NoneType
: None, a special value that represents the absence of a value, e.g.None
.
These are the main built-in data types in Python. There are also other data types available, such as datetime
, timedelta
, and enum
, which are part of the Python standard library.
Conclusion :
Mastering Python involves understanding its various features and nuances. On Day 4, we delved into global variables and data types, which are fundamental concepts in Python programming. By grasping how global variables work and familiarizing yourself with Python's diverse data types, you are building a solid foundation for more advanced topics. Keep practicing and exploring these concepts to enhance your Python skills further.