![]() |
|
Tutorial Python Lesson 1 - The Basics and Variables - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Tutorial Python Lesson 1 - The Basics and Variables (/Thread-Tutorial-Python-Lesson-1-The-Basics-and-Variables) |
Python Lesson 1 - The Basics and Variables - JMSPTGammer - 11-06-2013 Hey! In this thread I'm going to teach you a bit about Python! I started learning this week and I want to share with you guys. I am new to Python too, so I hope we can learn from each other. On this thread i will be covering: - print function; - variables; - declaring variables and printing them; - making a calculator; - and a final exam. First, download Python IDLE 2.7 from the website (just google it). Once you install it, open Python IDLE and click File->New Window. Now you can start coding! Print Function This is the most basic function in Python: the print function. All you have to do is write print and the text between " " or ' '. Example: Code: print "Hello World!"Code: print 'Hello World!'You can also print variables which i will be teaching you ahead. Variables and Declaring them A variable is a memory that keeps a value. To declare a variable, all you need to do is write the name of the variable and assign something to it, so it's the name of the variable, an equal sign and the value you are assigning to the variable. When assigning a number, you are using an integer. Example: Code: example=1Example: Code: example2="Hello World"So know that you know how to declare variables, you can print them. To do it so, all you have to do is write print and the name of the variable. Example: Code: print exampleCode: print example2You can also declare a variable to be something that the user writes, so input. You do it by naming the variable and making it equal to something, as always, and then you write input, for numbers, or raw_input, for words and letters. After input, you need () and inside, you can write the text displayed before the user enters the value to assign to the variable. Example: Code: example3=input("Write a number...")Code: example4=raw_input("Write a name...")Making a Simple Calculator To make a calculator, we have first to understand how it works. First, you need at least three variables:
So we will first declare them, but don't forget that the first two variables need to be assigned by the user, so we are going to use input: Code: num1=input("Insert First Number")
num2=input("Insert second Number")Now that we have assigned both the input variables, we will need to show the result, but i want to show all the possible results, so we will make 4 more variables: Code: add=num1+num2
sub=num1-num2
multiply=num1*num2
divide=num1/num2So know that we have all the variables needed, we will just print the results (i am going to make it a little more complex, but easy way) Code: print str(num1)+" "+"+"+" "+str(num2)+" "+"="+" "+str(add)
print str(num1)+" "+"-"+" "+str(num2)+" "+"="+" "+str(sub)
print str(num1)+" "+"*"+" "+str(num2)+" "+"="+" "+str(multiply)
print str(num1)+" "+"/"+" "+str(num2)+" "+"="+" "+str(divide)So, in the end, the code should be like this (or similar): Code: num1=input('Write one number:')
print num1
print
num2=input('Write another number:')
print num2
print
add=num1+num2
print str(num1)+" "+"+"+" "+str(num2)+" "+"="+" "+str(add)
print
sub=num1-num2
print str(num1)+" "+"-"+" "+str(num2)+" "+"="+" "+str(sub)
print
multiply=num1*num2
print str(num1)+" "+"*"+" "+str(num2)+" "+"="+" "+str(multiply)
print
divide=num1/num2
print str(num1)+" "+"/"+" "+str(num2)+" "+"="+" "+str(divide)And now, just to finish the calculator, we add a simple sentence in the end, so that the app only closes when the user presses the key: Code: print
print raw_input("Press another key to continue...")What have you learned? So know that i though you everything planned on the summary, I'm going to give you a challenge, a very easy one. You will have to make an application with this parameters:
I hope you liked and then send your "APPS" for me to see them! :biggrin: |