chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial Python Lesson 1 - The Basics and Variables filter_list
Author
Message
Python Lesson 1 - The Basics and Variables #1
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!"
or
Code:
print 'Hello World!'
To print a blank space, just write print.
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=1
You can also declare text to a variable, using a string:
Example:
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 example
or
Code:
print example2

You 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...")
or
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:
  • One variable to be the first number;
  • Another, to be the second number;
  • And the result(if you want to make a calculator that show all the possible results, you'll need a variable for the add result, the subtract, the multiply and the division)

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/num2

So 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:

  1. ask name from user and print the name after;
  2. ask from user two numbers;
  3. and print all the possible results.

I hope you liked and then send your "APPS" for me to see them! :biggrin:
Learning Python

[Image: python-logo-master-v3-icon.png][/align]

Reply






Users browsing this thread: 1 Guest(s)