print(1+3)
4
print(3*4)
12
print(3*4+2)
14
The results of the calculation can be moved into a variable.
boxA = 3*4+2
print(boxA)
14
Reading a variable can be combined with arithmetic.
boxA = 1
boxB = boxA + 2
print(boxB)
3
A single variable can be modified with arithmetic many times. This example is similar to counting. If all '1' values change to '2', what happens?
boxA = 3
boxA = boxA + 1
print(boxA)
boxA = boxA + 1
print(boxA)
boxA = boxA + 1
print(boxA)
4 5 6
The "+" sign can be used to combine strings. This is a bit different than math, because the strings are just hooked together to make a longer string.
print('socks' + 'shoes')
socksshoes
Strings can be combined with variables.
boxA = 'socks'
boxB = boxA + 'shoes'
print(boxB)
socksshoes
The "+" can happen many times.
boxA = 'start'
boxA = boxA + 'B'
print(boxA)
boxA = boxA + 'C'
print(boxA)
boxA = boxA + 'D'
print(boxA)
startB startBC startBCD
Strings can be combined with numbers. The "str()" means change the number into a string.
boxA = 'start'
count = 1
print(boxA + str(count))
count = count + 1
print(boxA + str(count))
count = count + 1
print(boxA + str(count))
start1 start2 start3
Multiple calculations can happen.
boxA = 'start'
count = 1
boxA = boxA + str(count)
print(boxA)
count = count + 1
boxA = boxA + str(count)
print(boxA)
count = count + 1
boxA = boxA + str(count)
print(boxA)
start1 start12 start123
Naming things is very important in programming. The more that you program, the better you will get at naming things.
There are common forms of names that programmers start to use. Some of these common names are used below.
The previous example code used names like drawer1 and boxA. These are not good names because programmers already know that these are variables that store things.
To store a count of the number of socks, it could be named count. If it was the only variable in the program, this might be ok, although it is difficult to tell what this is counting.
count = 1
If we also needed to store the count of shoes, then the count variable would not work.
count = 2 # Is this socks or shoes?
count = 4 # This changes count, so we overwrote socks or shoes?
When a program gets more complicated, then names can be similar to other names, and then the variables must be given more descriptive names. You might see all of the following forms of names while programming. It is best to decide which form of name to use in your programs.
It is very important to not have duplicate variables to hold the same thing. Otherwise the count of one of the variables might be changed, and the other variable might not, which can be very confusing.
CountOfSocks = 1
socksCount = 1
socks_count = 1
NumberOfSocks = 1