bool, int, double, float, char, void
and some other 'special' types. Note: a string
is a special kind of variable, made of up many char
values. See Arrays, below.Numbers
, Strings
, and Booleans
to be primitive types and abstracts the difference between int
, double
, or float
and string
or char
.Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables. ~ Tutorialspoint - Python Variable Types​
person1
, person2
, person3
... when does it end?string
is actually an array of char
variables. type name[size];
Java: type[] name = new type;
sequence
. The most basic sequence is a list
. Lists in Python act very similar to arrays in Javascript. They can be modified, contain any type of variable or object, and they have many built-in methods for utilization.string
is actually an array of chars
. So, the string "Hello" is actually a char
array of size 5 where char[0] = 'H'
, char[1] = 'e'
, char[2] = 'l'
, etc.[x, y]
, such that each element in the first index is, itself, an array.If...else
statements are the most basic tool for creating multiple pathways of code. There can also be shortcuts if you are comparing a simple boolean or want to combine items with And
or Or
boolean logic.ternary
operator. It is used for simple yes/no situations and is worth learning.Switch
or Case
statement if you are going to have many else if
statements or the condition is not inherently boolean in nature (like the value of a string, for example).dictionary
and call it a switcher
. You can read more about that on your own:10/i
for i
= 1 to 10 (Javascript shown)10/i
for i
= 1 to 10 (Python)foreach
loop, which is very handy. See the interactive sample below:for
loop is great when you have a set number of times you want to loop. However, loops don't have to run a specific number of times. There are two ways to loop based on a condition (true or false) instead of a value.while
loop is not guaranteed to run any number of times, even once. It depends on a specific condition and will continue to loop while that condition is true
- get it?false
. Infinite loops are very, very bad.myRandomNumber
inside the loop, we might have an infinite loop. I say might because there is no guarantee the loop runs in the first place. myRandomNumber
could start at a value less than 10. If that's the case, the loop never runs.break
and continue
commands for while loops as well.while
loop and it works extremely similarly to any other language. Here is the syntax:expression
is true, loop through the statements below it.break
and continue
commands for while loops as well.while
loop has a twin sibling - the do while
loop. This slight adjustment to the while
loop guarantees the code within the loop will run at least once.while
condition loops around the do
statement above it.do...while
loop construct. The short version of the story is that there has never been an agreed-upon syntax that matches the indented formatting of the Python language. Furthermore, it can easily be replicated or 'faked' with something like this:void
. It is also important to note that every C++ program has a main()
function and is compiled from top-to-bottom (not interpreted). The compiler needs to know the user-defined functions exist before compiling the main function.type nameOfFunction(type parameter1, type parameter2, ...) {
// Code execution
return [value matching function type]
}
avg
function after the main
. There is a way around this, however. We can prepare the compiler for the existence of our function with a function prototype
. That's fancy speak for a quick pre-declaration. Let's see the example again with our avg
function declared after main
.main()
function. There is a terminology difference - in Java a user-defined function is called a method. This is due to the fact that Java is a true object-oriented language and not a functional programming language.modifier returnType nameOfMethod(type parameter1, type parameter2, ...) {
// Code inside the method
return [optional value matching method type];
}
main()
method.function nameOfFunction(parameter1, parameter2, ...) {
// Content inside the function
return [optional value];
}
undefined
.def
keyword to define functions.def name_of_function(parameter1, paramter2, ...):
statement_1
statement_2
....
[optional return]