0% found this document useful (0 votes)
5 views45 pages

Python 1 Introduction

The document provides an introduction to Python programming, highlighting its interpreter-based nature, ease of learning, and popularity in deep learning applications. It covers fundamental concepts such as variable types, basic arithmetic operations, error handling, and type conversion. Additionally, it includes examples of Python syntax and functions, including input handling and string formatting.

Uploaded by

sihyun6876
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views45 pages

Python 1 Introduction

The document provides an introduction to Python programming, highlighting its interpreter-based nature, ease of learning, and popularity in deep learning applications. It covers fundamental concepts such as variable types, basic arithmetic operations, error handling, and type conversion. Additionally, it includes examples of Python syntax and functions, including input handling and string formatting.

Uploaded by

sihyun6876
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Software Design

Python 1
Eun-Sol Kim
Hanyang University
Python ? (1)
• Interpreter based programming language
• Command line-based operation
• cf: compiler-based language (C/C++ languages)
• Easy to learn (most popular)

• Deep learning algorithms are usually implemented using Python!


2022 March & 2025 March TIOBE Index

[Link]
Python ? (2)
• Powerful and convenient data structures and grammar
• List, dictionary types
• Memory allocation is simple for programmers.
• Data types are automatically identified and converted.
• Variable declaration is simple.
• Python versions
• 3.12.2 (2024.03. latest version)
Python Installation
• Official website: [Link]
Colab
Simple Python Examples (1)

command
Output for the command

• print ( )
• A python function to display (output) arguments.
• String (문자열)
• A list of characters surrounded by single or double quotation marks
Simple Python Examples (2)
Simple Python Examples (3)

• Distinguishing numbers or string


• We can print more than one arguments using print function
Simple Examples (4)

• print ()
• With “*” operator, we can repeatedly print strings
• We need to consider spacing words
Simple Examples (5)

• Easily concatenate strings


Errors (1)

• NameError
• If some typos are included variables or functions names
Errors (2)
Errors (3)

• SyntaxError
Edit Window (1)
• Beyond the shell interpreter, we can write a python program
using a file.

New File
Edit window
Edit Window (2)
• Write codes in the file
• Save the file
• ”Run Module”
• If we want to check the intermediate results, we should use
appropriate display function like “print()”
Memory and Variables

김은솔
이상화
Variable
• Variable ?
• Name to stored data(value) in the memory
• Method to access(접근) to stored data in the program
• Starting programming is to define the variables to be used in the
program
• Basic 3 variable types:
• integer (정수), float (실수, 부동소수), string (문자열)
• Memory that is allocated is different for each type.

Memory address Stored value(data)


Variable: STDID 0x12ABFFF0 202012345
0x12ABFFF4
0x12ABFFF6 Lee
Variable: name
. .
. .
Code Examples - integer type variable
Variable declaration
• Variable declaration (변수 선언) or definition (정의)
• Generate a new variable
• A memory space is allocated for the variable
• The initialization value is stored in the memory that the declared variable
indicates.
• Python needs the initialization (초기화) when a variable is declared.

Name of variable = (initial) value


‘=‘ : Assign the right-side value to the left-side variable(memory).
Not equality
Basic Variable Types (기본 변수형)
• Integer (int, 정수형)
• 0, 333, 45, 123456789000000000L (long integer)
• Typically, 4bytes (32bits) memory allocation
• Floating point (float, 실수형/부동소수형)
• 12.34, 3.0, 6.33333333333, 1.6e-19
• Typically, 4/8bytes (32/64bits) memory allocation
• String
• Enclosed by ‘ ’ or “ ” : “Lee”, ‘_abcd! + ?$%^’, ‘123’
• Adaptive memory allocation with respect to the length of string
• Python automatically recognizes the variables type with the initial
value.
• But we must remember the types of variables that we made!!
Code Examples - string type variable
Code Examples - float type variables

Float type is prior to the integer type.


Code Examples - variables
Variable Naming Rule (변수 명명규칙)
• Use alphabet characters, numbers(0~9), ‘_’ (under bar)
• No special characters:
• !, @, #, $, %, ^, &, *, (, ), ?, /, \, ~, =, +, -, space
• Differentiate lower from upper cases
• Lee, lee
• Start with alphabet characters and ‘_’
• OK: Lee3, _Lee04,
• No: 3Lee, L-ee04, abcde!, Lee 5 (file name is OK)
• Clear meaning, separation
• name1, var1, x, y, python_score, grade
• _PI = 3.141592
Basic Arithmetic Operation

김은솔
이상화
Basic Operations of Numbers
• Operator (연산자) : +, -, *, /
• Addition, subtraction, multiplication, division

Depends on the Python version, you can see an integer result.


3 Special Operators
• ** : power (지수연산, most priority)
• >>> 2**4*3 ( = (2*2*2*2)*3 ) -> 48
• % : modulus (나머지 연산, same priority with *, /)
• number1 % number2 (number2 != 0)
• >>> 6%4 -> 2
• >>> 3*6%4 ( = (3*6)%4 ) -> 2
• >>> 3*(6%4) -> 6
• >>> 4%3.1 ->0.89999999999999
• >>> -6%4 -> 2
• //: Quotient (몫, same priority with *, /)
• 20//6*3 -> 9
• 20*3//6 -> 10
Operation Order (연산순서/우선순위)
• Priority:
• ** > *, /, %, // > +, -
• Left to right for the same priority
• Follow the mathematical rules
• >>> 3+5*4 -> 23 (*, / priority)
• >>> (3+5)*4 -> 32 (parenthesis () priority)
• >>> 3/6*4 -> 2.0 (left priority)
• Cf: In some Python version, it can be printed as 4/6 = 0
• Use ( ) not to be confused!
Increment/Decrement Operators
• Increment (증가)
• >>> a = 5
• >>> a = a+1 (new variable ‘a’ declaration, new memory)
• >>> a += 1 (previous variable ‘a’ reassignment, same memory,)
• Decrement (감소)
• >>> b = 7
• >>> b = b-2 -> b=5 (new memory allocation)
• >>> b -= 1 -> b=4
• Multiplication and division
• >>> a *=3 (=> a = a *3)
• >>> b /= 2.0 (type conversion) -> b = 3.0
Exponential Expression (지수표현)

• Too large or too small number representation


• 1.6e-19, 3.0E10
• Automatically expressed to float type
• >>> print (2.5e3 * 4.0E2) -> 1000000.0 (not exponential)
• >>> print (2.5E10.0*4.00000) -> 10000000000000000.0
print ( ) Format (1)
• Use % format to include the variables into the string
(format specifier)
• Integer type: %i
• Float type: %f
• String type: %s

print ( “string … %i string…” %variable)

integer variable is added to the string,


and the integrated string is printed.
print ( ) Format (2)
• Print the result of “a+b” in two ways
• String concatenation
• String formatting

Source code Shell printing result


print ( ) Format (3)
• Print more than two variables in two ways
• String concatenation
• String formatting

Source code Shell printing result


print ( ) Format (4)
• Define the precision
• %.df
• For examples, %.2f, %.4f ….

Source code Shell printing result


Input and Type Conversion
김은솔
이상화
How to get data during running program?
• Variables that are declared in the source code
• Data (integer, string…) have been stored in the source code using variables.
• We have to modify the source code to get new data!

u How to get new data when running the source code ?


u input ( ) function: keyboard input data
u File input
input( ) function (1)
• Python embedded function for keyboard string input
• Only string type input
• Cursor is appeared on the Shell window
• Return(반환) the keyboard typed string
• The returned string should be stored in a variable !
• Syntax: input ( “optional_string” )
• The “optional string” is displayed on the shell.
• Usage example
• >>>string1 = input ( )
• >>>string2 = input ( “Enter your last name: “ )
input( ) function (2)
• Example
• Declare a variable and store the string from input ( )
type ( ) function
• Syntax: type (variable)
• Show the data type of variable
1) Declaration of integer type variable
2) Declaration of float type variable
3) Declaration of string type variable

4) Declaration of string type variable


Type Conversion (1)
• Problem: input ( ) only takes string type input
• How to get the integer and float types?
• => Data type conversion
• Python provides some type conversion functions.
• integer to float
• float to integer
• Discard below decimal point (소수점 아래 버림)
• Integer to string
• Float to string
• Including the decimal point
• String to integer/float
• If possible, in the string (“123a.9b” => ?? )
Type Conversion : int ( )
• Syntax: int ( variable or value )
• Conversion the variable (or value) to integer type
• Return the converted integer value

1) String to int conversion

2) Conversion error
Type Conversion : float ( )
• Syntax: float ( variable or value )
• Conversion the variable (or value) to float type
• Return the converted float value

1) String to float conversion

2) integer to float conversion

3) float to int conversion


Type Conversion : str ( )
• Syntax: str (variable or value)
• Conversion the variable/value to string type
• Return the converted string
Type Conversion: Examples

You might also like