Вы находитесь на странице: 1из 25

Python Environment Setup (Windows) and Python I

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:

Downloaded from the offi cial website of Python


The checkbox with the text "Add Python 3.6 to PATH" (at the bottom) must be
checked. 

When you have checked the box, click on the Install Now link in the middle of the screen.

Click "Install Now"


The screen below should appear and start installation. This may take a while.

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)

※ When  'python' is not recognized...  is displayed


There is a possibility that you have forgotten to check Add Python 3.6 to PATH when installing
Python. Re-open the file that you first downloaded and
click Uninstall. 

Select Uninstall
After this, re-open the file and re-install Python once again.

2. Running Python Code


In this section, you'll actually write Python on your own computer and practice running it.
First, make a folder on your desktop to save your code. As in the image below, right-click the
desktop and create a folder called python_lesson.
Next, create a Python file and start writing Python code. For this, you will need a tool called a  text
editor. A text editor (or just "editor") is where you write code like what you see in the middle of the
screen when doing an exercise on Progate.

Progate exercise page


There are many types of editors but here we will use a free editor called  Atom. People who have not
yet installed Atom can reference the section "1. The Text Editor" of the article  How to Make a
Website with HTML & CSS on your Computer (Windows) .
Try opening Atom. Click File at the top left of the screen and choose Open Folder....
Click Open Folder...
A screen to choose the folder will be shown, so select and open the python_lesson folder that you
created on the desktop.
Now let's create a new Python file. Right-click python_lesson on the left side of the screen in Atom
and choose New FIle.

Click New File


Name the file script.py like in the image below and then press enter.
I
nsert the file name script.py
If a File is Given the Wrong Name
When you create a file with the wrong file name, you can always change it. Right-click on the file
name you want to change in the sidebar and choose "Rename" to change it.
Write Python code in the script.py file that you created. In the file, copy and paste the code
prepared below:
print('Hello, world!')

print(1 + 2)

Insert code in script.py


Just writing the code will not save the changes. In the screen where you wrote the code,
press S while pressing Ctrl (Ctrl + S).
After you have saved the code, try running script.py. Re-open the Command Prompt that you used
in the last section and run the following command.
cd Desktop

Next, run the following command:


cd python_lesson

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

print('You will buy ' + str(count) + ' apples')


print('The total price is ' + str(total_price) + ' dollars')

if money > total_price:


print('You have bought ' + str(count) + ' apples')
print('You have ' + str(money - total_price) + ' dollars left')
elif money == total_price:
print('You have bought ' + str(count) + ' apples')
print('Your wallet is now empty')
else:
print('You do not have enough money')
print('You cannot buy that many apples')
Strings
Let's Print Characters
Let's run our first Python program!
You can display characters by using print().
Characters inside the parentheses will be displayed in the  console.
Strings
In Python, a sequence of characters, like "Hello Python", is called a string.
A string needs to be enclosed in single  ' or double " quotes.
The output will be the same either way.
If you don't put it in quotes, you will get an error.

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.

Difference between Strings and Integers


Strings and integers are interpreted differently in programming.
Like the images below,  3 + 5  will print  8 , which is the result of the addition. However,
if you enclose it in quotes and make it a string, the output will stay as   3 + 5 .

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.

How to Name a Variable ?


You can pick any name for your variable, but there are some rules.
For example, you cannot start a variable name with a number.
Also, when a variable name contains more than two words, like  user_name, you should separate
them with _.
Importance of Variables (1)
So far, we have learned how to use and name variables. But why do programmers use them?
One reason is that labeling data makes it easier to tell what the value represents, making the code
easier to read.

Importance of Variables (2)


There are other advantages of variables:
・They allow you to use the same data repeatedly.
・When you want to change the value of a variable, you only need to change it once.

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.

Updating the Value of a Variable ( 3 )


You can use shorthand like the image below when updating a variable that has an integer value.
The same syntax applies for all kinds of calculations.

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.

Differences in Data Types


Different types have different behaviors.
For example, print(5 + 7) calculates the sum while print('5' + '7') concatenates strings.

Type Conversion: str()


Like the image on the left, you can't concatenate strings and integers because
they have different data types.
You first have to convert the integer to a string, using str().
This is called type conversion.
Type Conversion: int()
You also can't perform calculations on a string. You have to convert it to an integer using  int().

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

# if x equals 70, print 'x is 70'


if x == 70:
print('x is 70')

# if y does not equal 40, print 'y is not 40'


if y != 40:
print('y is not 40')
Booleans
True and False
Let's try to understand how conditional expressions work.
When you print a condition like score == 100, True will be printed like the image on the right.
What is this value "True”?

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 .

if Statements and Boolean Values


Let's jump back to the if statement to see how it interacts with boolean values.
The code below the if statement is executed when the condition returns  True, and not executed when it
returns False.

Comparison Operators (<, <=, >, >=)


There are other operators you can use to compare values.
Just like math, you can use < and > to compare numbers.
You can also use >= and <= if you want to make the comparisons inclusive.

Summary of Comparison Operators


Check the image below to review the comparison operators that are oft en used!

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

if money >= apple_price:


print('You can buy an apple')
# When the condition above is False, print 'You do not have enough money'
else:
print('You do not have enough money')
elif Statements
elif ( 1 )
You can use elif to add alternative conditions to the control flow. Check the example of elif below!

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.

Printing the Payment Amount


First, let's print the money required to buy some apples!

Getting Input Changing the Number of Apples


In the last lesson, the number of apples had already been decided.
Let's change it so that you can choose the amount of apples you'll buy.
Getting Input
input() allows you to enter letters in the Console when running the code, and receive the value entered.
By writing variable = input('the string you want to show in console'), the entered value will be assigned to the variable.

Type Conversion of Input Values


The value you receive from input() will be a string, even if you enter a number.
If you want to use it as an integer to do some calculation, you have to convert it to an integer, like the image below.

Control Flow More Conditions!


Finally, let's use control flow to tell whether you have enough money to buy the specified number of
apples.

End of Python I

Вам также может понравиться