intro to variables programming

2
Variables and Data Types Python is a powerful, flexible programming language you can use in web/Internet development, to write desktop graphical user interfaces (GUIs), create games, and much more. Python is: • High-level, meaning reading and writing Python is really easy—it looks a lot like regular English! • Interpreted, meaning you don't need a compiler to write and run Python! You can write it here at Codecademy or even on your own computer (many are shipped with the Python interpreter built in— we'll get to the interpreter later in this lesson). • Object-oriented, meaning it allows users to manipulate data structures called objects in order to build and execute programs. We'll learn more about objects later. • Fun to use. Python is named after Monty Python's Flying Circus, and example code and tutorials often refer to the show and include jokes in order to make learning the language more interesting. This course assumes no previous knowledge of Python in particular or programming/computer science in general. Instructions: Click Save & Submit Code to continue. Variables (1st lesson) Creating web apps, games, and search engines all involve storing and working with different types of data. They do so using variables. A variable stores a piece of data, and gives it a specific name. Code: var varName = data; Example: a. var myName = "Lee"; b. var myAge = 30; c. var isOdd = true; Python d. spam = 5 The variable spam now stores the number 5. Instructions: 1. Set the variable my_variable equal to the value 10. 2. Click the Save & Submit button to run your code. Hint: Make sure to put my_variable on the left side of the =, and 10 on the right. You will notice that the window says "None" in it when you run the code/press enter. This is the "result" of your code, but you can generally ignore it. More Variable Practice We have seen how to create a variable. But how do we use it? It is useful to think that any time you type the variable's name, you are asking the computer to swap out the variable name and swap in the value of the variable. ---------------------------------------------- One of the most basic concepts in computer programming is the variable. A variable is a word/name/identifier that hangs onto a single value. For example, let's say you needed the number 5 for your program, but you're not going to use it immediately. You can set a variable, say spam, to grab the value 5 and hang onto it for later use, like this: spam = 5

Upload: maaz

Post on 05-Jan-2016

217 views

Category:

Documents


0 download

DESCRIPTION

Variable clearly explained.

TRANSCRIPT

Page 1: Intro to Variables Programming

Variables and Data Types Python is a powerful, flexible programming language you can use in web/Internet development, to write desktop graphical user interfaces (GUIs), create games, and much more. Python is: • High-level, meaning reading and writing Python is really easy—it looks a lot like regular English! • Interpreted, meaning you don't need a compiler to write and run Python! You can write it here at Codecademy or even on your own computer (many are shipped with the Python interpreter built in—we'll get to the interpreter later in this lesson). • Object-oriented, meaning it allows users to manipulate data structures called objects in order to build and execute programs. We'll learn more about objects later. • Fun to use. Python is named after Monty Python's Flying Circus, and example code and tutorials often refer to the show and include jokes in order to make learning the language more interesting. This course assumes no previous knowledge of Python in particular or programming/computer science in general. Instructions: Click Save & Submit Code to continue.

Variables (1st lesson) Creating web apps, games, and search engines all involve storing and working with different types of data. They do so using variables. A variable stores a piece of data, and gives it a specific name. Code: var varName = data; Example:

a. var myName = "Lee"; b. var myAge = 30; c. var isOdd = true;

Python d. spam = 5 The variable spam now stores the number 5. Instructions:

1. Set the variable my_variable equal to the value 10. 2. Click the Save & Submit button to run your code.

Hint: Make sure to put my_variable on the left side of the =, and 10 on the right. You will notice that the window says "None" in it when you run the code/press enter. This is the "result" of your code, but you can generally ignore it.

More Variable Practice We have seen how to create a variable. But how do we use it? It is useful to think that any time you type the variable's name, you are asking the computer to swap out the variable name and swap in the value of the variable. ----------------------------------------------

One of the most basic concepts in computer programming is the variable. A variable is a word/name/identifier that hangs onto a single value. For example, let's say you needed the number 5 for your program, but you're not going to use it immediately. You can set a variable, say spam, to grab the value 5 and hang onto it for later use, like this: spam = 5

Page 2: Intro to Variables Programming

Declaring variables in Python is easy; you just write out a name/identifier, like spam, and use = to assign it a value, and you're done!

Data Types Great! We can now summon the value 10 by calling out the name my_variable whenever we need it. In this case, the data type of my_variable is an integer (a positive or negative whole number). There are various data types: integers (int in Python language), floats (decimal/fractional numbers written with a decimal point, like 1.970), strings (sequences of characters, like the letters a-z, spaces, and even numbers. These are all strings: "Ryan", "4" and "What is your name?" Strings are extremely useful as labels, names, and content for your programs. Remember a string might not always be a word—you can put almost any character in between quotes to make a string) and booleans (which can be True or False. By default ???????). Computer programs, in large part, are created to manipulate data. Therefore, it's important to understand the different types of data (or "data types") that we can incorporate into our programs. Never use quotation marks (' or ") with booleans, and always capitalize the first letter! Python is case-sensitive (it cares about capitalization). We'll use quotation marks when we get to strings, which we'll cover in the next unit.

So spam = 5 Variable Sign to assign it a data/value Data (Data type is integer)