Академический Документы
Профессиональный Документы
Культура Документы
In this article, you will learn how to run (execute) Python code on your own PC rather than within
Progate in the browser.
Generally speaking, the environment in your own computer used to write code and develop
websites or software is called a local environment and preparing an environment for development
is called (local) environment setup.
After preparing your local environment, you will be able to write Python code and develop your own
programs at any time and in any place so long as you have your computer with you.
As you read this article, try to put what you are learning into action immediately to start developing
a local Python environment!
What You Need
Your own PC
Basic knowledge of Python
1. Installing Python
First, start by installing Python on your PC. From the link below, access the Python offi cial
website: https://www.python.org/
When you place your cursor on Downloads (image 1) at the top of the screen, the menu as can be
seen in the image below will be displayed. Click the gray button labeled Python 3.6.5 (image 2) on
the right side of the menu. (The number 3.6.5 may differ.)
The download will start once you click the button. When it is over, open the file.
You should see the following screen:
When you have checked the box, click on the Install Now link in the middle of the screen.
In
stalling...
Finally, when the screen below appears, you know you have finished installation! Click the X button
at the top right and close the screen.
Installation finished!
Now, let's check whether you can actually use Python in your PC. In order to use Python, you must
use a tool called Command Prompt.
In the search bar at the bottom left of the PC screen, type in Command Prompt. When the
application Command Prompt appears, double-click to open it.
A black screen like the one you can see below should appear:
In this screen, you will be able to send various commands to the computer. Here, let's check
whether Python is properly installed. Type the command below and press enter.
Python –version
If Python 3.6.5 is displayed as in the image below, you will know Python is properly installed.
After running the command: python --version (https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/document/16/ 1
538629353732.png)
Select Uninstall
After this, re-open the file and re-install Python once again.
print(1 + 2)
With this, you successfully moved the code to the python_lesson folder. (If you would like to learn
more about the cd command, try the Command Line Study I lesson).
In command prompt, you will be able to run the Python script by running the following command:
python file_name
In this case, replace "file_name" with script.py (the name of your Python script) and run it, like so:
python script.py
Did Hello, world! and 3 appear on the terminal as in the image below?
Result of script.py
Congratulations, you did it! You ran Python code on your own computer!
Python I
Python is a powerful language that is used in websites, machine learning and more. In this lesson we'll learn
the basics of the language.
Welcome to Python
Let's learn the basics of Python, one of the most popular programming languages in the world.
At the end, we'll be creating a simple app to calculate your shopping cost.
Let's get started!
Getting Started
About Python
Python is a programming language that is simple and easy to understand.
It is used in web development, machine learning, statistical processing, and more.
In this lesson, we will create a program that calculates your shopping cost. ----
apple_price = 2
money = 10
input_count = input('How many apples do you want?: ')
count = int(input_count)
total_price = apple_price * count
Comments
Adding # at the beginning will make the entire line a comment.
Comments will not be executed when running code, so you can use them to leave notes.
Integers
You can use numbers like integers in programming.
Unlike strings, they don't need to be enclosed in quotes.
You can add and subtract integers just like you do in math.
The spaces before and after operators are not required, but they will make the
code easier to read.
More Calculations
In Python, you can do other calculations like multiplication and division, but with different symbols
from what you would use in math.
* is for multiplication and / is for division.
You can also calculate the remainder of a division using %.
Variables---What is a Variable?
Next, we'll learn about variables.
A variable is like a box with a name in which you can store a value.
Defining a Variable
To store a value in a variable, you need to define a variable.
You can do this in the following format: variable_name = value.
= in programming does not mean "equal". It means assign the value on the right to the
variable on the left.
Note that variable names don't need to be enclosed in quotes.
Printing a Variable
Now let's learn how to print the name variable.
You can do this by writing print(name).
Note that if you enclose the variable in quotes, like print('name'), name becomes a string instead of a
variable.
Therefore, the output will be "name", not the value of the variable.
Updating Variables
Updating the Value of a Variable ( 1 )
This time we will update the value of a variable that is already defined.
You can overwrite a variable simply by assigning a new value to it.
In the images below, the output has changed as the value was updated.
Updating the Value of a Variable ( 2 )
In order to add some number to a variable, you can assign the sum of the variable and the number
to the variable itself.
This may seem strange, but remember that = means assign, not equal.
String Concatenation
The + that we used for calculations also lets us combine strings.
Combining strings is called string concatenation.
String concatenation can be used with strings and variables that have string values.
Data Types
So far, we have worked with two kinds of values, strings and integers. These are called data types.
There are other data types in Python, but let's cover these two first.
if Statements
Control Flow
We'll learn about control flow from here.
In programming, we often want to run different code based on whether it satisfies a certain
condition.
For example, you might want to say 'Great job!' only when the score is 100%.
The if Statement
By using the if statement, you can write code that gets executed only under a certain condition.
You can create if statements by writing if, followed by a conditional expression and a colon :.
The code on the next line will only run when the condition is true.
Conditional Expressions
There are many operators to create conditional expressions.
Let's first take a look at equality operators.
We can use == to see if two values are equal, and != to see if two values are not equal.
Indentation
You must indent your code when writing if statements.
The indented code below the if statement will run only when the condition is true.
If you forget to indent your code like the image on the right, it will run whether or not the condition
is true.
Example---
x = 7 * 10
y=5*6
Booleans
True is a value of the Boolean data type.
The Boolean data type has only two values, True and False .
The value True is returned when a condition is true, and False if not.
Keep in mind that you must capitalize the first letter of True and False .
Example—
x = 10
# if x is greater than 30, print 'x is greater than 30'
if x > 30:
print('x is greater than 30')
money = 5
apple_price = 2
# if money is equal to or greater than apple_price, print 'You can buy an apple'
if money >= apple_price:
print('You can buy an apple')
else Statements What Happens When the Condition is False
By using if statements, you can now run code only when the condition is True.
Next, let's learn how to create control flow statements that can run different code when the
condition is False.
else
Using the else statement, you can add some code you want to run when the condition of the if statement
is False.
Example---
money = 2
apple_price = 4
elif (2)
You can add elif as many times as you want.
However, keep in mind that only the code that returns True for the first time will be executed.
Combining Conditions
and
Let's learn how to combine multiple conditional expressions!
You can use the and operator to combine conditions.
For example, Condition 1 and Condition 2 will return True only when both Condition 1 and Condition 2 are true.
or
You can use the or operator in a similar way. Condition 1 or Condition 2 will return True if either Condition 1 or
Condition 2 is true.
This means that the combined condition will be True if at least one of the conditional expressions is True.
not
By using not, you can negate a condition.
The means that it'll be False if the conditional expression is true and it'll be True if it is false.
x = 20
# if x ranges from 10 to 30 inclusive, print 'x ranges from 10 to 30'
if 10 <= x and x <= 30 :
print('x ranges from 10 to 30')
y = 60
# if y is less than 10 or greater than 30, print 'y is less than 10 or greater than 30'
if y < 10 or 30 < y :
print('y is less than 10 or greater than 30')
z = 55
# if z is not equal to 77, print 'z is not 77'
if not z == 77 :
print('z is not 77')
Calculating Prices Calculating Shopping Prices
Combining what we have learned so far, we'll create a simple program that calculates
shopping prices.
Like the image below, we'll print the results of calculation according to the number of
apples entered in the Console.
End of Python I