Changes

Jump to navigation Jump to search

Workshop: Introduction to programming (with Python)

986 bytes added, 21:52, 9 September 2019
no edit summary
 == Variables ==
Now that we understand the basic workflow of python (create code in text editor → execute in command prompt), we can start discussing the different functions and capabilities of Python. We can start this discussion with variables. You can think of variables as the objects that store information for our program. A variable has two basic parts; '''A name''' (which we will use to refer to the variable) and '''A value''' (the info we care about). Besides those two parts, variables can hold different types of information.
'''Types Of Variables:'''
# Strings - A sequence of characters (ex: "Python is fun!")# Integers - Whole numbers (ex: 3, 10, 999)# Floats - Numbers w/ a decimal (ex: 1.0, 3.14, 7.77)# Lists - An sequential list of other variables (floats, integers, strings, more lists, or a mix) (ex: ['cat', 'dog', 999, 3.14, variable1])
'''Updating Variables:''' To update a variable (change it's value later on in the code) we simply need to redefine it's value. You can do this by: variableName = [new value] '''Casting Variables:''' When we get into more complex implementations our variable type will become very important. Ask yourself 'what is the difference between these two codes?' # float sum = 1.5 + 3# int sum = 1.5 + 3 Answer: 1.5 + 3 = 4.5 but 4.5 is a float value. So for code #1 the answer is 4.5, but the "sum" variable in code #2 is an integer so the answer will be rounded down to 4.
It because of cases like these that sometimes we implement a technique known as casting. Casting simply means changing the type of information a variable holds. With casting we can turn a int to a float, float to an int, str to an int, etc. We cast by writing the variable type we want ("float", "int", "str", etc.) followed by the variable/value we want to cast enclosed by parenthesis.
Examples:
Casting Variables* float(3) → 3.0* int(2.9999) → 2* int("1964") → 1964
== '''The first step''' is to download Python- ==
If you haven’t yet got python, the latest official installation packages can be found here:
http://python.org/download/
118
edits

Navigation menu