The first 25% of any college-level Programming Fundamentals course covers data types, variables, operators, and decision structures. Hopefully the next two blog posts will give you a solid introduction to concepts and you can take it from there (Google is your friend! For more depth in paper-form Starting Out With Python by Tony Gaddis is a good resource too)
DATA TYPES - strings, numbers, boolean, arrays, objects(this)
VARIABLE DECLARATIONS -
COMPARISON OPERATORS -
LOGICAL OPERATORS -
Variables and their Data Types
const greeting = "Hello fellow coder" const question = "What's your name?" const age = 36
age = 35 favNumber = "What is your favorite number?"
A variable is defined as a name that represents a value stored in the computer’s memory. The values with quotes around them are
string data types and the value that
age is referencing is an
int or integer or number data type.
const to declare our variables. In the Python example, we have no keyword before the variable declaration. It is different for all languages.
const result = greeting+age; console.log(result);
>>> "Hello fellow coder36"
result variable, so we’ll get
"Hello fellow coder36"
If we don’t concatenate (use the
+ sign) but use a
, comma to dilineate arguments within the
console.log(), we’ll print out two different values of two separate data types:
>>> "Hello fellow coder" 36
If we try to concatenate a string and a number in Python, this would return a
print(favNumber + age) TypeError: can only concatenate str (not "int") to str
This is one reason why data types are important to understand in each language!
Operators: Logical and comparison
We’ve seen use of the
You can use
=== to make comparisons between values. These are read as completely different operators than the
= (single equals sign).
# First, assign a value to the variable age = 36.5 # Then, compare it with another value print(age == 35)
Since we’re using
==, it compares the value of
age which is 36.5 to the
int 35. Since they’re not the same value, it returns false.
const above) you should not use key words as variable names. At first you may do this accidentally, but as you use a language you’ll learn which are reserved words to avoid and how to use them properly.
Floating point and integer division
One point to be careful of is
float division and
In Python, there is one symbol for floating-point division:
/ and another for integer division:
# Divide two integers using the floating point divisor 5 / 2 # Get a floating point answer >>> 2.5
# Divide two integers using the integer point divisor 5 // 2 # Get an integer answer >>> 2
As you see, it chops of the
.5 for integer division. This is not precise if you’re doing calculations!
# Is the first evaluation the same as the second? (5//2) == (5/2) # Is the first eval less than the second? (5//2) < (5/2) # Is the first eval more than the second? (5//2) > (5/2) # Does the first eval not equal the second? (5//2) != (5/2)
The only ones that would print
true are the second statement and the last statement.
The use of
! is both involved in a comparison with
!= not equal to, and logical operator
!. In Python, it’s literally the word
not. Just like
and in Python!
// Using the modulus operator const leftover = 17 % 3 >>> 2 // Incorperating ! !leftover >>> false
leftover contains the integer 2, it’s a
Falsy values are as follows:
"" (empty string),
// Using the modulus operator const leftover = 10 % 2 >>> 0 // Incorperating ! !leftover >>> true