How do i create a variable in python


#1

Im not sure how to create a variable in python
Lesson 16

my_string = 100
print (8)
print "my_string.upper()


#2

The one above is how. All that needs thought is the naming. A label my_string would imply a string expression as a value, not a number. This will become clear as you progress.

my_string = "a quoted string literal"

my_number = 100

is_number = True

and so on.


#3

mtf was right on how you create variables. There are however, a couple simple rules.

  1. Variables must start with a letter or an underscore. ( this_ or _this )
  2. If rule 1 is true, you can also use numbers throughout the name, although I don't like it and I've been told it's frowned upon. ( th1s or thi5 )

Also, don't forget that variables are case sensitive. ( THIS is not this )


#4

These are some ways:

myName = "ibmbng"

or

my_Name = 'ibmbng'

myAge = 1000

my_list = [ ]

or

myList = [50, 30, 1, 1000, "x", "y" ]

or

myList = [50, 30, 1, 1000, 'x', 'y' ]

my_boolean = True

my_Boolean = False

myset = { }

or

mySet = { }

............ and many more.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.