Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)

Similar presentations


Presentation on theme: "Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)"— Presentation transcript:

1 Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)

2 - 2 - Objectives We will be reviewing what we learned last week Strings / What can you do with them Variables And user Input Questions & Answers

3 - 3 - Follow Up We set up Python We went over strings and variables –You define a string with the quotations “I AM A STRING –You can add strings together i.e. (“test“ + “mest” = “testmest”) –You can store values in things called variables –You can add variables together

4 - 4 - Strings A ‘string' is simply a list of characters in order. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. For example, “Hello, my name is Jeff!” all of the letters like H, e, l, l, 0, … are ‘strung” together to form a string. Ok how can I write a “String” and use it in python?

5 - 5 - Defining/Writing a String There are 3 ways of defining/writing a string, –1. ‘I am a string‘ –2. “I am a string” –3. “””I am a string””” These practically mean the same thing, but later we’ll discus the differences and how we’ll use them. Everything inside of the quotations is the string, the quotations wont be interpreted as a string.

6 - 6 - Using Strings First try typing hello into the command line and see what the out put is. If all goes to plan you’ll get an error. That is because hello is not defined in python as anything so the only thing python knows what to do with it is to just throw out an error, Now try typing ‘hello’ with the quotation thingies in front of and behind the word your trying to type out. You’ll get a response from python telling you that it understood what was trying to be done and it prints out ‘hello’ with no issues.

7 - 7 - String Examples Try to predict the out put of each string when we type them in –“test” –‘Hello, World!’ –“I am 4 years old” –‘I Like to cause errors” –“4” –‘2 + 2’

8 - 8 - Printing Out In python there is a built in function called print(). Print() is the command that allows you to display text on to the command line. The Print() command is called a function, and I’ll be getting into later what a function is, and we’ll get to writing out own functions just like the print() function; In this lecture I won’t be explaining how it works, I’ll just show you how to use it.

9 - 9 - Print() Use Ok so how to we use a print(), first of all you could just type print() in the command line, and it will do something. Lets say you want to do more. The print() function allows for data to be put in side of it, like a String, or an Int, or a Float. All of these work –print(‘I am a string’) –print(12) –print(3.141592653589793266) All of those work

10 - 10 - Printing Examples Try and guess the out puts of each print function –print(“Hello, World!”) –print(123) –print(“I am 4 Years Old”) –print(“2 + 2”) –print(2 + 2)

11 - 11 - Variables What are variables? Are they the same thing as a variable in math? Yes they are, kind of, but better. You can use them to store values and use them later. Open your command prompt / terminal. Then type x = 10. you can then type x again and the number 10 will spit out. This is the most basic example / use for a variable.

12 - 12 - Variable Names You don’t have to name your variables X, you can name them however you like. –For example type var = 123, You have just created another variable. –You can print out the number 123 by typing out var. There is also one other thing where your other variables aren’t effected. –Type X again and you’ll get the value for X back. You can do this as many times as you like. You can name your variables almost anything, you can’t have numbers in the beginning of your variable name, and there are some key words where you can’t name your variables. Keep in mind that variable names are case sensitive. –Here are some examples var = 10 Var1 = 11 x = 1 X = 2

13 - 13 - Variable uses Variables have a specific way to set a value to them. –First you type the name of your variable –Then you use the equal sign. (The equal sign in Python means to assign a value NOT COMPARATIVE) –Then you put the value at the end of your equal sign Try to create you own variables. To get back the value just type the name of your variable and Python will give back the value. You can overwrite existing variables but just assigning a new value to it. –Type x and it should give back a value –Now assign X a new value by typing X = -12 –If you print out X it will print out -12. When you overwrite a variable in Python the last value will be lost in cyberspace.

14 - 14 - How else can I use a Variable Variables can store more than just numbers they can store STRINGS also. –Type var = “STRING” and then print it out –You can see that var has just stored a string, you can over write this variable with a number as such var = 10 You can also store an answer of an arithmetic problem –Type var = 10 + 20 and then print it out. –You can also type var2 = “test1” + “test2” and print it out.

15 - 15 - How else can I use a Variable 2 Variables because variables can store values they can also do other things that you can normally do with numbers/strings –You can add variables together, make a variable called x and set it as 15, then make a variable called y and set it as 10, you can now type x + y and get back a number –You can also try making two new variables and store string values in them then add them together. –Also try creating 2 variables like x and y and add them and set them equal to a new variable like newVar = x + y

16 - 16 - Why use variable? It makes sense to think that there is no point in making a variable when you can just type the number in your code and have it mean the same thing. You would be right in that case. What variable are used for is when you (as a programmer) don’t know what the value of the variable is going to be. This doesn’t make sense right now because you haven't used anything like that.

17 - 17 - User Input This is an example of not knowing what the value of the number will be when writing the program. For example lets say I want to make a calculator program that asks the user for two numbers and then adds them together. As a programmer you have no idea what the numbers are going to be. All you can do is make two variable and then add them together and display the total.

18 - 18 - User Input Usage In Python there is a built in function (similar to the print function) called input(). This input function tells the program to wait for the user to type something and displays the value of what they just typed –For example type input() in the command prompt / terminal and watch python wait for you. You can type what ever you want in their. Once you press enter Python will display what you had just typed. –You can also type in a string inside of the brackets like input(“Enter Your Number: ”). All this does is it displays that string before it waits for the user input, it is the same as the print function. Type typing input(“Enter Value: ”) and then enter a value. Try to guess what the output is going to be.

19 - 19 - Lets try writing an input program in IDLE The Goal with this program is to prompt the user to enter his first name and last name and have python print it out.

20 - 20 - Questions & Answers

21 © 2014 Chris Trenkov


Download ppt "Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)"

Similar presentations


Ads by Google