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

Tut Pythonbasic

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

Tut Pythonbasic

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

Python basics

Presenter : Ziwen Cai


ziwencai@link.cuhk.edu.cn

1
Programming basics
 code or source code: The sequence of instructions in a program.
 syntax: The set of legal structures and commands that can be
used in a particular programming language.
 output: The messages printed to the user by a program.

 console: The text box onto which output is printed.


 Some source code editors pop up the console as an external
window,
and others contain their own console window.

2
Compiling and interpreting
 Many languages require you to compile (translate) your program
into a form that the machine understands.
compile
source code byte code outpu
Hello.java execute
Hello.class t

 Python is instead directly interpreted into machine instructions.

interpret
source code outpu
Hello.py t

3
The Python Interpreter


Python is an interpreted >>> 3 + 7
language
10
The interpreter provides

>>> 3 < 15
an interactive True
environment to play with
the language >>> 'print me'
'print me'
Results of expressions are

>>> print('print me')
printed on the screen
print me
>>>

4
Expressions
 expression: A data value or set of operations to compute a value.
Examples: 1 + 4 * 3
42
 Arithmetic operators we will use:
+ - * / // addition, subtraction/negation, multiplication, division
% modulus, a.k.a. remainder
** exponentiation

 precedence: Order in which operations are computed.


 ** has a higher precedence than * / // % + -
 * / // % have a higher precedence than + -
1 + 3 * 4 is 13

 Parentheses can be used to force a certain order of evaluation.


(1 + 3) * 4 is 16

5
Float division
 When we divide numbers with / , the quotient is always a float
number

6
Integer division (floor)
 When we divide integers with // , the quotient is also an integer.
 If any of the numbers is float, it returns output in float.
 Examples:

35 // 5 is 7
3 52
4 ) 14 27 ) 1425

84 // 10 is 8
12 135

156 // 100 is 1 2 75
54

35 // 5.0 is 7.0 21

84 // 10.0 is 8.0

 The % operator computes the remainder from an integer division.


3 43
4 ) 14 5 ) 218
12 20
2 18
15
3

7
Math commands
 Python has useful commands (or called functions) for
performing calculations.
Constant Description
Command name Description
e 2.7182818...
abs(value) absolute value
pi 3.1415926...
ceil(value) rounds up
cos(value) cosine, in radians
floor(value) rounds down
log(value) logarithm, base e
log10(value) logarithm, base 10
max(value1, value2) larger of two values
min(value1, value2) smaller of two values
round(value) nearest whole number
sin(value) sine, in radians
sqrt(value) square root

 To use many of these commands, you must write the following at


the top of your Python program:
from math import * 8
8
Numbers: Floating
Point
 int(x) converts x to an >>> 1.23232
1.2323200000000001
integer
>>> print(1.23232)
 float(x) converts x to
1.23232
a floating point >>> 1.3E7
 can use type() 13000000.0
>>>int(2.0)
to see the type of
2
a variable >>>
float(2)
2.0
>>>
type(2.0) 9
Variables
 variable: A named piece of memory that can store a value.
 Usage:

Compute an expression's result,

store that result into a variable,

and use that variable later in the program.

 assignment statement: Stores a value into a variable.


 Syntax:
name = value

 Examples: x = 5
gpa = 3.14

x 5 gpa 3.14
 A variable that has been given a value can be used in expressions.
x + 4 is 9

 Exercise: Evaluate the quadratic equation for a given a, b, and c.


10
Exercise: Quadratic equation

11
Example

>>> x = 7
>>> x
7
>>>
x+7
14
>>> x
=
'hell
o'
>>> x
'hell
o'
>>>
12
print
 print() : Produces text output on the console.
 Syntax:
print("Message")

print(Expression)
 Prints the given text message or expression value on the console, and
moves the cursor down to the next line.
print(Item1, Item2, ..., ItemN)
 Prints several messages and/or expressions on the same line.

 Examples:
print("Hello, world!")
age = 45
print("You have", 65 - age, "years until retirement")
Output:
Hello, world!
You have 20 years until retirement 13
Example: print Statement


Elements separated by commas print with a space
between them

>>> print('hello')
hello
>>> print('hello',
'there')
hello there

To print without newline: print(‘hello’, end =‘’)

>>> print('hello', end = '')


hello>>>

14
input
 input : Reads a string from user input.
 You can assign (store) the result of input into a variable.

 Example:
age = int(input("How old are you? "))
print("Your age is", age)
print("You have", 65 - age, "years until retirement")
Output:
How old are you? 53
Your age is 53
You have 12 years until retirement
 Exercise: Write a Python program that prompts the user for
his/her amount of money, then reports how many Nintendo
Switches the person can afford, and how much more money
he/she will need to afford an additional Switch.

15
Input: Example
print("What's your name?")
name = input("> ")

print("What year were you born?")


birthyear = int(input("> "))

print("Hi ", name, "!", "You are ", 2022 - birthyear)

% python3 input.py
What's your name?
> Michael
What year were you
born?
>1980
Hi Michael! You are 16
Q&A

17

You might also like