Skip to content

melaniehoff/code-words-spells

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Words Spells: Variables & Functions in Python & Javascript

Javascript vs. Python

To read a great article on the difference between Javascript and Python, check here!

  • My top-level distinction is Javascript is for making dynamic websites. Javascript lives and breathes in the Browser. - Python, is way more of an all-purpose programming language for millions of computational uses including on the web (in servers but not in the browser).

Variables

“For magic consists in this, the true naming of a thing.” ― Ursula K. LeGuin, A Wizard of Earthsea

  • variables are named containers that hold information. When we name something we can refer to it later. Naming is powerful.
  • words in laguage are like variables. They are named containers that hold meaning. To name something is declare the existance of the thing we are naming. names for words and variables don't emerge from the ether, they are collectively constructed and reaffirmed by people. what are the social histories of the words you speak?

Example Variables in Python:

dog = "woof"
my_fav_number = 7

Example Variables in Javascript:

var dog = "woof";
var myFavNumber = 7;
  • Notice the quotations for a variable that contains a line of text (variable type is called a string). Notice that for a variable type of number, there are no quations. Why do you think that is?
  • Notice that in python vs. there are different naming conventions. snake_case vs. camelCase. Why do you think this is? (hint there's not a clear hard and fast answer, it's really just differnet naming conventions for different languages!)

"When I use a word," Humpty Dumpty said, in rather a scornful tone, "it means just what I choose it to mean—neither more nor >less." "The question is," said Alice, "whether you can make words mean so many different things." "The question is," said >Humpty Dumpty, "which is to be master—that's all."

Variable types

  • Variables in any language have different data types. Two common types are called strings and numbers.
  • A string is a line of text like "cat", with quotation marks around it. A variable data type of number is represented by numerical character(s) that you can perform math on.
  • Notice that x = 2 is a data type of number but x = "2" is actually a data type of string. The former could be added and subtracted, the latter is simply the character "2"

Python:

dog = "woof"
my_fav_number = 7

type(dog)
type(my_fav_number)

Javascript:

var dog = "woof";
var myFavNumber = 7;

console.log(typeof dog);

console.log(typeof myFavNumber);

Functions

  • A function is a block of reusable code (like a shortcut!) that is used to perform an action. Functions provide modularity for your project and the ability to easily reuse and repurpose code. Technically, any piece of code (from your website to the code that runs Facebook) can be written without using functions but you wouldnt want to do this because your code would then be so long!

  • Lanhguages like Python and Javascript gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Function in Python:

dog = "woof"

def print_me( my_var ):
   print (my_var)
   return

print_me(dog)

Function in Javascript:

var dog = "woof";

function printMe(my_var){
  console.log(my_var);
}

printMe(dog);

If Statements

Python:

dog = "woof"
my_fav_number = 7

if type(dog) == type(my_fav_number):
  print("they are the same data type")
else:
  print("they are different data types!")

Javascript:

var dog = "woof";
var myFavNumber = 7;

if (typeof dog == typeof myFavNumber) {
  console.log("they are the SAME data type!");
} else {
  console.log("they are DIFFERENT data types!");
};

words have power to make things so.
code has power to make things so.
spells have power to make things so.

when we write code for ourselves and our friends, code that this is written with care, with intention and with intimacy, this literally the stuff of magic

— ⟴ ↭ ⥉ ⬼ ↯ ↬ (@melanieh0ff) January 10, 2020

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published