Python Variabelen
- Previous Page Python Commentaar
- Next Page Python Datatypes
Variabelen aanmaken
Variabelen zijn containers voor datawaarden.
anders dan andere programmeertalen heeft Python geen variabelenverklaringcommando.
Variabelen worden pas aangemaakt wanneer ze voor het eerst waarde toegewezen krijgen.
Example
x = 10 y = "Bill" print(x) print(y)
Variabelen vereisen geen specifieke typeverklaring en kunnen hun type zelfs wijzigen nadat ze zijn ingesteld.
Example
x = 5 # x is van type int x = "Steve" # x is nu van type str print(x)
String-variabelen kunnen worden declareren met enkele aanhalingstekens of dubbele aanhalingstekens:
Example
x = "Bill" # is hetzelfde als x = 'Bill'
Variabelen naam
Variabelen kunnen korte namen gebruiken (zoals x en y) of meer beschrijvende namen (age, carname, total_volume).
Python variabelen naamgevingsregels:
- Variabelen moeten beginnen met een letter of een underscore
- Variabelen kunnen niet beginnen met een cijfer
- Variabelen kunnen alleen letters, cijfers en underscores bevatten (A-z, 0-9 en _)
- Variabelen zijn naamgevoelig voor hoofd- en kleine letters (age, Age en AGE zijn drie verschillende variabelen)
Onthoud dat variabelen naamgevoelig zijn voor hoofd- en kleine letters
Waarde toekennen aan meerdere variabelen
Python staat u toe om in één regel waarde toe te wijzen aan meerdere variabelen:
Example
x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z)
U kunt dezelfde waarde in één regel toekennen aan meerdere variabelen:
Example
x = y = z = "Orange" print(x) print(y) print(z)
Variabelen weergeven
Python's print
Zinnen worden meestal gebruikt om variabelen weer te geven.
Om tekst en variabelen te combineren, gebruikt Python +
Tekens:
Example
x = "awesome" print("Python is " + x)
U kunt ook het +-teken gebruiken om variabelen met een andere variabele te sommen:
Example
x = "Python is " y = "awesome" z = x + y print(z)
For numbers,+
Characters are used as mathematical operators:
Example
x = 5 y = 10 print(x + y)
If you try to combine strings and numbers, Python will give an error:
Example
x = 10 y = "Bill" print(x + y)
Global Variables
Variables created outside the function (as shown in all the above examples) are called global variables.
Global variables can be used by everyone inside and outside the function.
Example
Create a variable outside the function and use it inside the function:
x = "awesome" def myfunc(): print("Python is " + x) myfunc()
If you create a variable with the same name inside a function, then the variable will be a local variable and can only be used within the function. The global variable with the same name will remain unchanged and retain its original value.
Example
Create a variable with the same name as a global variable inside a function:
x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
global keyword
By default, when a variable is created inside a function, it is a local variable and can only be used within that function.
To create a global variable inside a function, you can use the global keyword.
Example
If you use the global keyword, then the variable belongs to the global scope:
def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
Additionally, if you want to change a global variable inside a function, please use the global keyword.
Example
To change the value of a global variable inside a function, please use the global keyword to refer to that variable:
x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
- Previous Page Python Commentaar
- Next Page Python Datatypes