Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Variables
#1
Code:
x = 3 # x is of type int
x = "Mike" # x is now of type str
print(x)
The value of  the variable changes


Code:
a = "wonderful"
print("Life is " + a) #result "Life is wonderful"
concatenating a string with the value of a variable it has to be of a string type too.


Code:
a = "Life is "
b = "wonderful"
c =  a + b  #you can concatenate two variables
print(c)


Code:
a = 5
b = 10
print(a + b) #With numbers the +  works as a mathematical operator.
Code:
a = 9
b = "David"
print(a + b) #This however will throw an error, you cannot concatenate numbers with strings in Python.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)