Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Lecture01 Interpreter Calculator

Python introduction

Uploaded by

botchannel280
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture01 Interpreter Calculator

Python introduction

Uploaded by

botchannel280
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Python Programming

Hyuntae Cho
Dept. of Digital Content
Tongmyong University
The designer of Python, Guido van
Rossum, at OSCON 2006
Intro. To Python
• Python:
– 귀도 반 로섬(Guido Van Rossum) announced Python 0.9.0 in 1991.

– Naming:
• “Python” was derived from UK TV show “Monty Python's Flying Circus”
• “Python” is a kind of largest snakes.

2
Intro. To Python
• Python is a high-level, general-purpose programming language.
• Its design philosophy emphasizes code readability with the use of
significant indentation.

• Features:
– Interpreter language
• A language that allows you to interpret and execute source code line by line and
immediately check the results
– high level programming language
– Independent to platform, object oriented, and dynamic type/interactive
language

3
Python Installation and Version Check
• Open Terminal program
– Search “cmd” (in the left bottom of the screen)

• Check Python version

4
If Python is not installed?
• Python download
– Python web page: (http://www.python.org)
– Click [Downloads] menu → ‘Python 3.12.2’

5
Installation process
• Execute the downloaded file after download completes
– Then, follow instructions below.

6
Installation process
• After installation completes
– Execute “Python 3.12 (64-bit)”

Interpreter mode

7
Python Programming using IDLE
• IDLE: (Integrated Development and Learning Environment)

• It gives us methods to make simple codes


• Execution:
– Window start menu → click [Python 3.12] Folder → click [IDLE (Python 3.12
64-bit)]

8
Python Programming using IDLE
• Using IDLE Interpreter mode
→ Python shell

• Programming codes is so long~. So interpreter mode is inconvenient

9
Python Programming using IDLE
• We can make files to build Python codes

Code Editor

10
Python Programming using IDLE
• Save files
– Click [File] → [Save] → you can see [save as ] window below → choose “your
folder” to save files
– → “hello.py” → click [save]

11
Python Programming using IDLE
• Code Execution in Code Editor
– Click [Run] menu → choose [Run Module] →
– You can see the result below
Code Editor

Python shell
(Interactive mode)

12
Ok we are ready to make Python codes

13
Interactive mode
• When commands are read from a tty, the interpreter is said to be in
interactive mode. In this mode it prompts for the next command
with the primary prompt, usually three greater-than signs (>>>);

• Execute “IDLE (Python)


– The interpreter prints a welcome message stating its version number and a
copyright notice before printing the first prompt:

14
Interactive mode
• for continuation lines it prompts with the secondary prompt, by
default three dots (...).

result

15
Python Programming
(Using Python as a Calculator)

Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Using Python as a Calculator
• Let’s try some simple Python commands.
• Start the interpreter and wait for the primary prompt, >>>. (It
shouldn’t take long.)

17
Numbers
• The interpreter acts as a simple calculator: you can type an
expression at it and it will write the value.
• Expression syntax is straightforward: the operators +, -, * and / can
be used to perform arithmetic;
• parentheses (()) can be used for grouping.
• For example:

The integer numbers (e.g. 2, 4, 20) have type int, the ones with a fractional
part (e.g. 5.0, 1.6) have type float.

Division (/) always returns a float.

18
Numbers
• Division (/) always returns a float.
• To do floor division and get an integer result you can use the //
operator;
• to calculate the remainder you can use %:

19
Numbers
• With Python, it is possible to use the ** operator to calculate powers:

• The equal sign (=) is used to assign a value to a variable. Afterwards,


no result is displayed before the next interactive prompt:

20
Numbers
• If a variable is not “defined” (assigned a value), trying to use it will
give you an error:

• There is full support for floating point; operators with mixed type
operands convert the integer operand to floating point:

21
Numbers
• In interactive mode, the last printed expression is assigned to the
variable _. This means that when you are using Python as a desk
calculator, it is somewhat easier to continue calculations, for example:

22
Numbers
• In addition to int and float, Python supports other types of numbers,
such as Decimal and Fraction.
• Python also has built-in support for complex numbers, and uses the j
or J suffix to indicate the imaginary part (e.g. 3+5j).

23
Python Programming
(Python Syntax)

Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Execute Python Syntax
• As we learned in the previous page, Python syntax can be executed
by writing directly in the Command Line:

• Python Indentation
– Indentation refers to the spaces at the beginning of a code line.
– Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
– Python uses indentation to indicate a block of code.

• Example:

Let’s try it
– Python will give you an error if you skip the indentation:
https://www.w3schools.com/python/tryp
ython.asp?filename=demo_indentation_t
est
25
Execute Python Syntax Let’s try it with your
code file (.py)
• Python Indentation
– The number of spaces is up to you as a programmer, the most common use
is four, but it has to be at least one.

– You have to use the same number of spaces in the same block of code,
otherwise Python will give you an error:
• Syntax error

26
Python Variables
• In Python, variables are created when you assign a value to it:

• We will learn more about variables in the Python Variables chapter.

27
Comments
• Python has commenting capability for the purpose of in-code
documentation.

• Comments start with a #, and Python will render the rest of the line
as a comment:

28
Python Programming
(Comments)

Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Comments
• Comments can be used to explain Python code.

• Comments can be used to make the code more readable.

• Comments can be used to prevent execution when testing code.

30
Creating a Comment
• Comments starts with a #, and Python will ignore them:

• Comments can be placed at the end of a line, and Python will ignore
the rest of the line:

• A comment does not have to be text that explains the code, it can
also be used to prevent Python from executing code:

31
Multiline Comments
• Python does not really have a syntax for multiline comments.

• To add a multiline comment you could insert a # for each line:

• Or, not quite as intended, you can use a multiline string.

• Since Python will ignore string literals that are not assigned to a
variable, you can add a multiline string (triple quotes) in your code,
and place your comment inside it:

32
Python Programming
(Variables)

Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Variables
• Variables are containers for storing data values.

• Creating Variables
– Python has no command for declaring a variable.
– A variable is created the moment you first assign a value to it.

• Variables do not need to be declared with any particular type, and can
even change type after they have been set.

34
Type Casting
• What’s data type?
– Integer: 1, 2, 3, 4, 5…..
– Float (decimal): 0.5, 1.25, 3.14, 105.25, …..
– String: “I am Gildong Hong”

• If you want to specify the data type of a variable, this can be done
with casting.

35
Get the type
• You can get the data type of a variable with the type() function.

36
Single or Double Quotes?
• String variables can be declared either by using single or double
quotes:

37
Case-Sensitive
• Case-Sensitive
– This will create two variables:

38
Variable Names
• A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume). Rules for Python variables:
– A variable name must start with a letter or the underscore character
– A variable name cannot start with a number
– A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
– Variable names are case-sensitive (age, Age and AGE are three different
variables)
– A variable name cannot be any of the Python keywords.

39
Variable Names
• Legal variable names:

• Illegal variable names:

40
Multi Words Variable Names
• Variable names with more than one word can be difficult to read.
• There are several techniques you can use to make them more
readable:

– Camel Case
• Each word, except the first, starts with a capital letter:

– Pascal Case
• Each word starts with a capital letter:

– Snake Case
• Each word is separated by an underscore character:

41
Many Values to Multiple Variables
• Python allows you to assign values to multiple variables in one line:

42
One Value to Multiple Variables
• And you can assign the same value to multiple variables in one line:

43
Unpack a Collection
• If you have a collection of values in a list, tuple etc. Python allows
you to extract the values into variables. This is called unpacking.

• Example:
– Unpack a list:

44
Output Variables
• The Python print() function is often used to output variables.

• In the print() function, you output multiple variables, separated by a


comma:

45
Output Variables
• You can also use the + operator to output multiple variables:

• For numbers, the + character works as a mathematical operator:

46
Output Variables
• In the print() function, when you try to combine a string and a
number with the + operator, Python will give you an error:

– Error

• The best way to output multiple variables in the print() function is to


separate them with commas, which even support different data
types:

47
Global variables
• Variables that are created outside of a function (as in all of the
examples above) are known as global variables.

• Global variables can be used by everyone, both inside of functions


and outside.

• Example:
– Create a variable outside of a function, and use it inside the function

48
Global variables
• If you create a variable with the same name inside a function, this
variable will be local, and can only be used inside the function. The
global variable with the same name will remain as it was, global and
with the original value.

• Example:
– Create a variable inside a function, with the same name as the global variable

49
Global Keyword
• Normally, when you create a variable inside a function, that variable
is local, and can only be used inside that function.

• To create a global variable inside a function, you can use the global
keyword.

• Example:
– If you use the global keyword, the variable belongs to the global scope:

50
Global Keyword
• Also, use the global keyword if you want to change a global variable
inside a function.

• Example:
– To change the value of a global variable inside a function, refer to the variable
by using the global keyword:

51
Conclusion

52

You might also like