일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 파이썬터틀
- 오블완
- springquickstart
- 스프링
- 비주얼스튜디오코드
- 프로그래밍
- 파이썬
- 티스토리챌린지
- 자바
- html
- vscode
- spring
- python
- 스프링퀵스타트
- 생각하는달리기
- 캐나다
- 캐나다일상
- 달리기
- 스타벅스주문
- springquick
- 파이썬기초
- 파일명일괄변경
- 이클립스
- CSS
- THINKINGRUN
- 파이참
- Java
- 영어스타벅스
- 코딩
- Eclipse
- Today
- Total
LIKE A DIAMOND
[Python] 001.Hello World! #codeAcademy 본문
#codeAcademyPython
Hello World!
If programming is the act of teaching a computer to have a conversation with a user, it would be most useful to first teach the computer how to speak. In Python, this is accomplished with the print
statement.
A print
statement is the easiest way to get your Python program to communicate with you. Being able to command this communication will be one of the most valuable tools in your programming toolbox.
---
2
Print Statements
There are two different Python versions. Both Python 2 and Python 3 are used throughout the globe. The most significant difference between the two is how you write a print
statement. In Python 3, print
has parentheses.
In this course we will be using Python 2. If you go on to write Python 3 it will be useful to note this key difference.
---
3
Strings
When printing things in Python, we are supplying a text block that we want to be printed. Text in Python is considered a specific type of data called a string. A string, so named because they're a series of letters, numbers, or symbols connected in order — as if threaded together by string. Strings can be defined in different ways:
Above we printed two things that are strings and then attempted to print two things that are not strings. While double-quotes (") and single-quotes (') are both acceptable ways to define a string, a string needs to be opened and closed by the same type of quote mark.
We can combine multiple strings using +
, like so:
This code will print out "This is a good string".
---
4
Handling Errors
As we get more familiar with the Python programming language, we run into errors and exceptions. These are complaints that Python makes when it doesn't understand what you want it to do. Everyone runs into these issues, so it is a good habit to read and understand them. Here are some common errors that we might run into when printing strings:
If the quotes are mismatched Python will notice this and inform you that your code has an error in its syntax because the line ended (called an EOL
) before the double-quote that was supposed to close the string appeared. The program will abruptly stop running with the following message:
This means that a string wasn't closed, or wasn't closed with the same quote-character that started it.
Another issue you might run into is attempting to create a string
---
5
Variables
In Python, and when programming in general, we need to build systems for dealing with data that changes over time. That data could be the location of a plane, or the time of day, or the television show you're currently watching. The only important thing is that it may be different at different times. Python uses variables to define things that are subject to change.
In the above example, we defined a variable called greeting_message
and set it equal to the string "Welcome to Codecademy!". It also defined a variable called current_exercise
and set it equal to the number 5.
---
6
Arithmetic
One thing computers are capable of doing exceptionally well is performing arithmetic. Addition, subtraction, multiplication, division, and other numeric calculations are easy to do in most programming languages, and Python is no exception. Some examples:
Above are a number of arithmetic operations, each assigned to a variable. The variable will hold the final result of each operation. Combinations of arithmetical operators follow the usual order of operations.
Python also offers a companion to division called the modulo operator. The modulo operator is indicated by %
and returns the remainder after division is performed.
In the above code block, we use the modulo operator to find the remainder of 15
divided by 2
. Since 15
is an odd number the remainder is 1
.
We also check the remainder of 133 / 7
. Since 133
divided by 7
has no remainder, 133 % 7
evaluates to 0
.
---
7
Updating Variables
Changing the contents of a variable is one of the essential operations. As the flow of a program progresses, data should be updated to reflect changes that have happened.
In the above example, we start with 50 fish in a local pond. After catching 10 fish, we update the number of fish in the pond to be the original number of fish in the pond minus the number of fish caught. At the end of this code block, the variable fish_in_clarks_pond
is equal to 40.
Updating a variable by adding or subtracting a number to the original contents of the variable has its own shorthand to make it faster and easier to read.
In the above example, we use the price of a sandwich to calculate sales tax. After calculating the tax we add it to the total price of the sandwich. Finally, we complete the transaction by reducing our money_in_wallet
by the cost of the sandwich (with tax).
---
8
Comments
Most of the time, code should be written in such a way that it is easy to understand on its own. However, if you want to include a piece of information to explain a part of your code, you can use the #
sign. A line of text preceded by a #
is called a comment. The machine does not run this code — it is only for humans to read. When you look back at your code later, comments may help you figure out what it was intended to do.
---
9
Numbers
Variables can also hold numeric values. The simplest kind of number in Python is the integer, which is a whole number with no decimal point:
A number with a decimal point is called a float. You can define floats with numbers after the decimal point or by just including a decimal point at the end:
You can also define a float using scientific notation, with e
indicating the power of 10:
---
10
Two Types of Division
In Python 2, when we divide two integers, we get an integer as a result. When the quotient is a whole number, this works fine:
However, if the numbers do not divide evenly, the result of the division is truncated into an integer. In other words, the quotient is rounded down to a whole number. This can be surprising when you expect to receive a decimal and you receive a rounded-down integer:
To yield a float as the result instead, programmers often change either the numerator or the denominator (or both) to be a float:
An alternative way is to use the float()
method:
---
11
Multi-line Strings
We have seen how to define a string with single quotes and with double quotes. If we want a string to span multiple lines, we can also use triple quotes:
This address spans multiple lines, and is still contained in one variable, address_string
.
When a string like this is not assigned to a variable, it works as a multi-line comment. This can be helpful as your code gets more complex:
---
12
Booleans
Sometimes we have a need for variables that are either true or false. This datatype, which can only ever take one of two values, is called a boolean. In Python, we define booleans using the keywords True
and False
:
A boolean is actually a special case of an integer. A value of True
corresponds to an integer value of 1
, and will behave the same. A value of False
corresponds to an integer value of 0
.
---
13
ValueError
Python automatically assigns a variable the appropriate datatype based on the value it is given. A variable with the value 7
is an integer, 7.
is a float, "7"
is a string. Sometimes we will want to convert variables to different datatypes. For example, if we wanted to print out an integer as part of a string, we would want to convert that integer to a string first. We can do that using str()
:
This would print:
Similarly, if we have a string like "7"
and we want to perform arithmetic operations on it, we must convert it to a numeric datatype. We can do this using int()
:
If you use int()
on a floating point number, it will round the number down. To preserve the decimal, you can use float()
:
---
14
Review
---
Finish Quiz Answer
#1skill_completed = str("Python Syntax") #2exercises_completed = 13 points_per_exercise = 5 #3point_total = 100 #4point_total += exercises_completed * points_per_exercise #5 #The amount of points for each exercise may change, because points don't exist yet#6print "I got "+str(point_total)+" points!"
'notUsed_STUDY' 카테고리의 다른 글
[파이썬002] 파이썬 터틀로 파이썬 언어 맛보기 (0) | 2020.04.07 |
---|---|
[파이썬001] 설치 및 Hello World, print (0) | 2020.04.06 |
python 004. 리스트 다루기b (0) | 2017.07.22 |
python 004. 리스트 다루기a (0) | 2017.07.18 |
python 003. 리스트 소개 (0) | 2017.07.15 |