Tut Pythonbasic
Tut Pythonbasic
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.
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
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
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
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
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
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 =‘’)
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("> ")
% python3 input.py
What's your name?
> Michael
What year were you
born?
>1980
Hi Michael! You are 16
Q&A
17