Python Language
Structure
#story of "hong gil dong" #(1)
name = "Hong Gil Dong" #(2)
age = 18
weight = 69.3
skill = ["sword","spear","bow","axe"] #(3)
power = [98.5, 89.2, 100, 79.2]
querySkill = raw_input("select weapon: ") #(4)
print "\n"
print "----------------------------------------"
print "1.name:", name #(5)
print "2.age:", age
print "3.weight:", weight
i=0
print str(123)
for each_item in skill: #(6)
(7) if(each_item == querySkill): #(8)
(9) print "4.armed weapon:",each_item, "[ power", power[i],"]"
print ">>>i am ready to fight"
(10) i = i+1 #(11)
print "----------------------------------------"
print "\n"
>>>
select weapon: sword
----------------------------------------
1.name: Hong Gil Dong
2.age: 18
3.weight: 69.3
4.armed weapon: sword [ power 98.5 ]
>>>i am ready to fight
----------------------------------------
Python
Language Structure
The
“IDLE” (Python
application) can be used to develop, run and debug a
program. The
“Ctrl+S” key
stores the program
and “F5” key run it. Let's now look at an example that has been developed in
IDLE.
(1) Comments: The lines starting with “#” are treated as comments in a program, and these are not executed. To comment out an entire paragraph, it must be enclosed in the [‘’’] symbol.
(2) Variable Declaration: The types of variables are not specified, and for Python only the name is declared.
(3) List: A list is enclosed in square brackets "[" and may be used as an “array”. The reference number starts from 0. The type is not specified, and it is possible to store strings and numbers together.
(4) Using the Built-in Functions: The built-in function “raw_input” is used here. This function receives user input and stores it in the variable “querySkill”
(5) Combining the String and Variable Value: A comma “,” makes it possible to combine the string and the Variable value.
(6) Loop: The “for” statement is a loop. The number of items in the “skill” list are repeated, and the start of the loop is represented by a colon “:”. There is no indication for the end of the loop, and the subroutines for the loop are separated by the indentation.
(7) The Program Block Representation: The “Space” or the “Tab” key represent a program block. Developers that are familiar with other languages may feel a little awkward at first. However, once used to it, you can feel that syntax errors are reduced and coding becomes simplified.
(8) Comparison and Branch Statement: It is possible to use an “if” statement to determine a “true” or “false” condition. The colon “:” specifies the start of the branch statement block, and in a manner similar to C and Java, a comparison uses the “==” symbol.
(9) Multiple Lines of Program Block Representation: If you use the same number of “Space” or “Tab” characters, the lines are regarded as part of the same block.
(10) New Program Block: If a smaller number of “Space” or “Tab” characters are used than a previous block, this indicates that the new lines correspond to a new program block.
(11) Operator: Similar to C and Java, Python uses the “+” operator. Python also uses the following reserved words, and these reserved words cannot be used as variable names.
Reserved Words
And |
del |
for |
is |
raise |
assert |
elif |
form |
lambda |
return |
break |
else |
global |
not |
try |
class |
except |
if |
or |
while |
continue |
exec |
import |
pass |
yield |
def |
finally |
in |
print |
|
Python is a language that dynamically
determines the type for a variable. When the variable name is first declared, the type of
variable is not specified, and Python will automatically recognize the type when
you assign the value of the variable and store it in memory. There are some
drawbacks in terms of performance, but this provides a high level of convenience
to the programmer. Python supports data types, such as the following.
Frequently Used Data types
Numerics |
int |
Integer |
1024, 768 |
|
float |
Floating-point |
3.14,
1234.45 |
|
complex |
Complex |
3+4j |
Sequence |
str |
Strings, Immutable
objects |
“Hello
World” |
|
list |
List, Mutable
objects |
[“a”,’’b”,1,2] |
|
tuple |
Tuple, Immutable
objects |
(“a”,”b”,1,2) |
Mapping |
dict |
Key
viewable list, Mutable objects |
{“a”:”hi”,
“b”:”go”} |
Branch Statements and Loop
In
addition to Java and C, Python supports branch statements and loops. The usage
is similar, but there are some differences in the detailed syntax. First, let's
learn the basic structure and usage of the branch statement.
if <Conditions comparison 1>:
Execution syntax 1
elif <Conditions comparison 2>:
Execution syntax 2
else:
Execution syntax 3
Python
uses a structure that is similar to
that of other languages, but it has a difference in that it uses “elif" instead of “else if”.
Next,
let's look at the loop. There are two kinds of loops: “while” and “for”. The
function is similar, but there are some differences in terms of implementation.
The most significant difference from other languages is that the “else”
statement is used at the end.
while |
for |
while <Execution syntax>: Execution syntax else: Execution syntax |
for <Variable> in <Object>: Execution syntax else: Execution syntax |
The “for” statement is used to repeatedly assigns an item to a variable for only the number of items contained in the object. It runs a statement every time that an item is assigned, one by one. When the allocation of the item is completed, the loop ends after executing the commands defined in the “else” statement.