0% found this document useful (0 votes)
4 views

02 PythonBasics

Uploaded by

fwwyg2chzy
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)
4 views

02 PythonBasics

Uploaded by

fwwyg2chzy
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
You are on page 1/ 33

Chapter 2

Python Basics
Prof. Hyeong-Seok Ko
Seoul National University
Dept. of Electrical and Computer Engineering
Contents
• Hello World
• IDLE (Python GUI)
• Value, Type, Variable
• Basic Operators
Hello World
• “Hello World” in Python Command Line
Hello World
• “Hello World” in IDLE (Python GUI)
IDLE (Python GUI)
• Integrated DeveLopment Environment (IDLE) for Python
– Multi-window text editor
• with syntax highlighting, auto-completion, smart indentation, and so on.
– Python shell with syntax highlighting
• IDLEs for other languages
– Visual Studio (C, C++, C#, ...)
– Xcode (C, C++, Objective-C, Java, ...)
– Eclipse (Mostly for Java)
– And many others!
Running a Source Code File (in IDLE)
• File > New File
• Type:
print("Hello, Python!")
print(42)
print(3.14, "Hello")
• File > Save As <hello.py>
• Press F5
Running a Source Code File (in cmd)

We will work mostly in IDLE, not in cmd.


Value and Type
• What is ‘Value’?
– "Hello, World" is a string value.
– 42 is a number value.
• What is ‘Type’?
– "Hello, World" is a string type (str)
– 42 is an integer type (int)
– 3.14 is a floating-point number type (float)
Value and Type
• “type” function
print(42, type(42))
print(4.2, type(4.2))
print("Hello", type("Hello"))
print(type("Hello"), type(type("Hello")))
Type Checking
• “isinstance” function
print(isinstance(42, int))
print(isinstance(42, float))
print(isinstance(4.2, int))
print(isinstance(4.2, float))
print(isinstance("Hello", str))
Boolean Type
• Data type with two values (True, False)
print(True, type(True))
print(False, type(False))
print(1<2, type(1<2))
print(1>2, type(1>2))
print(True or False, type(True or False))
print(True and False, type(True and False))
print(not True, type(not True))
Variable
• A variable is a name that refers to a value.
• The assignment statement creates a new variable and gives it a value.

variable

var = 10.0 assignment statement

print(var, type(var))
Variable

length = 10
print("Area : ", length*length)
print("Volume : ", length*length*length)
Operators
• Operators are special symbols that represent computations like
addition and multiplication.
• The values the operator uses are called operands.
– ex) What is the operator and operands in the following?
• b = 3**4
• a = 2 + 3
• a += 1 # a = a + 1
• a *= 2 # a = a * 2
• a /= 3 # a = a / 3
Examples
• >>> 1+1
• 2
• >>> a = 14
• >>> b = 32
• >>> a * b
• 448
• >>> c = b/a
• >>> print(c)
• 2.2857142857142856
Complex Examples
• >>> ((2.0+5.0)*(9.0+4.0))/10.0
• 9.0999999999999996
• >>> 5+4*8
• 37
Order of Operations
• PEMDAS and Left-to-Right
1. Parentheses
2. Exponentiation
3. Multiplication and Division
4. Addition and Subtraction

• Exmaple:
– 6**8/6 vs. 6**(8/6)
– Compare the results!
– 279936.0 vs. 10.902723556992836
Operations on Strings
• Addition
– "Hello, " + "World"
– Hello, World

• Multiplication
– "Fun"*3
– FunFunFun
– This is illegal: "Fun" * "3"
Comparison Operators
• >>> a = 8
• >>> b = 5
• >>> c = a < b
• >>> print(c)
• False
Comparison Operators
• Less-than: <
• Less-or-equal: <=
• Equal: ==
• Not-equal: !=
• Greater-than: >
• Greater-equal: >=
Logical Operators
• Not: not a
• Identity: a is b
• Identity*: a is not b
Logical Operators Example
• >>> a = False
• >>> print(not a)
• True
• >>> b = 42; c = 31
• >>> print(b is c) semi-colon for line
• False breaker
• >>> print(b is not c)
• True
Mathematical Operators
• Exponentiation (ab): a**b
• Modulo: a%b
• Floor-division: a//b
• Float-division: a/b
Mathematical Operators Examples
• >>> a = 2; b = 5
• >>> print(a**b)
• 32
• >>> print(b%a)
• 1
• >>> print(b//a)
• 2
• >>> print(b/a)
• 2.5
Dividing Integers and Floats
• >>> a = 42.0; b = 5.0
• >>> print(a/b)
• 8.4
• >>> print(a//b)
• 8.0
• >>> a = 42; b = 5
• >>> print(a/b)
• 8.4
Bitwise Operators

a = 53 0 0 1 1 0 1 0 1

b = 29 0 0 0 1 1 1 0 1
Bitwise And Operator: &

a = 53 0 0 1 1 0 1 0 1

b = 29 0 0 0 1 1 1 0 1

a&b = 21 0 0 0 1 0 1 0 1
Bitwise Or Operator: |

a = 53 0 0 1 1 0 1 0 1

b = 29 0 0 0 1 1 1 0 1

a|b = 61 0 0 1 1 1 1 0 1
Bitwise Xor Operator: ^

a = 53 0 0 1 1 0 1 0 1

b = 29 0 0 0 1 1 1 0 1

a^b = 40 0 0 1 0 1 0 0 0
Bitwise Inversion Operator: ~

a = 53 0 0 1 1 0 1 0 1
Two’s Complement.

?
~a = -54 1 1 0 0 1 0 1 0
Bitwise Shift Operators: >> and <<
• Right shift: a>>b
Shifts a by b bits
• Left shift: a<<b

a = 53
print(a>>1)
a = 53 0 0 1 1 0 1 0 1 print(a)

26
a>>1 0 0 0 1 1 0 1 0 53

What is the mathematical meaning of


right and left shift operator?
Homework
• Repeat the previous bitwise operations with a = 256, b = 29.
• Can you explain the results?

a = 256
b = 29
print(a&b)
a = 53 0 0 1 1 0 1 0 1 print(a|b)

0
b = 29 0 0 0 1 1 1 0 1 285
Comments
• Notes for Humans!
• Single-line comment (#)
# compute the average
avg = (a+b+c+d)/4.0

• Multi-line comment (""")


"""
This program computes the 2-D heat equation
using blah blah blah. See also Kim et al.
2010.
"""

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy