PowerPoint Presentation Algorithms Programming with Python Python Level 1– Lesson 2 Nguyễn Chí Thức gthuc l com Data types – string and integers 2.PowerPoint Presentation Algorithms Programming with Python Python Level 1– Lesson 2 Nguyễn Chí Thức gthuc l com Data types – string and integers 2.
Algorithms & Programming with Python Python Level 1– Lesson Nguyễn Chí Thức gthuc.nguyen@gmail.com 0986636879 Data types – string and integers Strings 'python' "programming" """ This is a sample of multiline string ""“ Integers 12 543 214 * 5233 352 // 34 452 % 12 "hello" + " world“ "My number is " + str(12345) Data types - float Floats 12.0 543.12 214.0 * 5233 352 / 34 131.12 – 124.31 Comments # this is the first comment spam = # and this is the second comment # and now a third! text = "# This is not a comment" Multiline comment ''' This is a comment line And this is another comment line ''' Input data into a program print('What is your name?') # read a single line and store it in the variable "name" name = input() print('Hi %s!‘ % name) input() returns a string Input data into a program name = input('What is your name?' ) print('Hi %s!‘ % name) Sum of two numbers What is the output of this program, if we enter the value for a, and for b? a = input() b = input() s = a + b print(s) Sum of two numbers What is the output of this program, if we enter the value for a, and for b? a = input() b = input() s = a + b print(s) 57 !!! Cast string of digits into an integer number a = int(input()) b = int(input()) s = a + b print(s) 10 Arithmetic operators >>> >>> >>> >>> 4.5 * 3.8 2017 + 2018 * (2017 + 2018) * ((3 + 7) * 10) / + Addition - Subtraction * Multiplication / Division % Modulus ** Exponent // Integer division 11 Cast string into a real number a = float(input()) b = float(input()) s = a + b print(s) 12 Discarding the fraction part print(int(1.3)) print(int(1.7)) print(int(-1.3)) print(int(-1.7)) 13 Discarding the fraction part print(int(1.3)) print(int(1.7)) print(int(-1.3)) print(int(-1.7)) # # # # gives gives gives gives 1 -1 -1 14 round function print(round(1.3)) print(round(1.7)) print(round(-1.3)) print(round(-1.7)) 15 round function print(round(1.3)) print(round(1.7)) print(round(-1.3)) print(round(-1.7)) # # # # gives gives gives gives -1 -2 16 Precision print(0.1 + 0.2) 17 Precision # gives 0.30000000000000004 print(0.1 + 0.2) 18 math module import math x = math.ceil(4.2) print(x) print(math.ceil(1 + 3.8)) 19 math module Return the floor of x, the largest integer less than or floor(x) equal to x ceil(x) Return the ceiling of x, the smallest integer greater than or equal to x sqrt(x) Return the square root of x log(x) With one argument, return the natural logarithm of x (to base e) With two arguments, return the logarithm of x to the given base e The mathematical constant e = 2,71828 sin(x) asin(x) pi Return the sine of x radians Return the arcsine of x, in radians The mathematical constant π = 3.1415 20 ... print(int (1. 3)) print(int (1. 7)) print(int( -1. 3)) print(int( -1. 7)) # # # # gives gives gives gives 1 -1 -1 14 round function print(round (1. 3)) print(round (1. 7)) print(round( -1. 3)) print(round( -1. 7)) 15 ... print(round (1. 3)) print(round (1. 7)) print(round( -1. 3)) print(round( -1. 7)) # # # # gives gives gives gives -1 -2 16 Precision print(0 .1 + 0.2) 17 Precision # gives 0.30000000000000004 print(0 .1 + 0.2) 18 ... division 11 Cast string into a real number a = float(input()) b = float(input()) s = a + b print(s) 12 Discarding the fraction part print(int (1. 3)) print(int (1. 7)) print(int( -1. 3)) print(int( -1. 7)) 13