© 2019 BrainStemSchool.com

December 13, 2016

December 9, 2016

December 7, 2016

Please reload

Literals and Variables

December 5, 2016

https://youtu.be/GYHyZxX3oNM

 

Some of my users gave me grief on the brevity of my last two videos so here's one that pushes the limit - running about 10 minutes.

 

Literals are any values you can see in the code itself.

 

For instance,

 

print("Hello, World") 

 

The string "Hello, World" is the literal.

 

Literal values can then be stored in variables and utilized:

 

name="Mike"

print("Hello,",name)

 

This is opposed to if we were to ask a user his name then print a personalized message:

 

name=input("What is your name?")

print("Hello,",name)

 

In this example the user's name never shows up in code. This program will take user input and output it. It can accommodate any name that anyone enters.

 

Literals can be of any type:

 

print(4)


print(2134.432)

 

Variables are containers for values. You can store a value in a variable and utilize that variable as if it were a literal value.

 

The assignment operator looks like the single equal sign. It reads and evaluates what is on its right, and assigns it to what is on its left.

 

***

Therefore: WE KNOW that if there is a single equal sign, the thing on its left MUST be a variable. Also WE KNOW that the thing on its right MUST be or evaluate to a value.

***

 

In this example we assign 123 to the variable x:

 

x=123

 

In this example we assign 3.1415926 to the variable pi

 

pi=3.1415926

 

In this example we assign the value 27 to the variable y

 

y=20+7

 

Do not confuse single and double equal signs. = assigns, == evaluates equality.

 

Finally, Python is not Math:  = assigns, it does not describe. That's why we can say:

 

x=5

x=x+1

print(x)

 

6 will be printed to screen...

1. 5 is assigned to x

2. 5+1 is evaluated and assigned to x (thereby removing the previous value of 5)

3. 6 is printed

 

 Next Post: Math (Built-in)

 

Share on Facebook
Share on Twitter
Please reload

Please reload

Recent Posts
Tags