AnIn111_C01_TypesAndTests.pdf

Full Transcript

Introduction to programming I.P.S.A. - Course AnIn 111 Course 1 : Introduction to the Copyright M. BONNEFOI Plan Execution (interpretation) of the python Basic syntax and types: integers...

Introduction to programming I.P.S.A. - Course AnIn 111 Course 1 : Introduction to the Copyright M. BONNEFOI Plan Execution (interpretation) of the python Basic syntax and types: integers 123 reels 1,14.103 strings "Bonjour" Basic testing: the if/else statement InterInteraction: inputs/outputs M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 2 Cours 01 : Introduction Python: a scripting language Mode of execution of a programming language: - compiled with a compiler that produces the executable ex : C, C++, Ada etc. - interpreted by a console (also called a 'virtual machine’) ex : Web( html, css, javascript, php...), python, bash etc. - compiled and interpreted ex : java 1] In development: In all cases, the 'source code' (also called 'script' for interpreted languages) is written in a 'text file'. In Python the script extension is '.py', for example 'test.py'. 2] Interactive: You can also directly execute instructions in the console. M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 3 Cours 01 : Introduction Python : installation / tests / Dev kit We launch a python 'console': → from another console like bash or power shell, dos...python3 → from the desktop manager: search for IDLE On your pc : Official site : https://www.python.org/ Dev kit : https://www.python.org/downloads/ (other distributions, e.g. iPython, Qpython (android)...) From a web page: Ex. IPython NoteBook: http://mooc.lrde.epita.fr/notebooks/Untitled1.ipynb M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 4 Cours 01 : Introduction Interactive Arithmetic Syntex Text M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 5 Cours 01 : Introduction Interactive Imaginaries Library Precision M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 6 Cours 01 : Introduction Variables We can store values in memory using variables: >>> a = 2 >>> b = 4 >>> c = a + b >>> print(c) 6 >>> name = "Alan" >>> surname = "Turing" >>> phrase = name + " " + surname + " and his machine !" >>> print(phrase) Alan Turing and his machine ! Variable names can be composed of letters, numbers and _. The only restriction is not to put a number at the beginning: 2tab No spaces and no ( { or [ 'Bests practices' : The name of a variable must be well chosen to make the code easy to understand. Their length should be proportional to the size of the code: Short code → short variable names Long and Complex code → long variable names M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 7 Cours 01 : Introduction Data types The different basic types of programming Nombers: Booleens → symbol bool True/False Integers → symbol int 123 ‘’Reels'' → symbol float 2,33.1023 The characters : A caracter → symbol char A string → string or str M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 8 Cours 01 : Introduction Dynamic types in python In Python, types are implicit. in c : int a = 12 ; in python : a = 12 in c : float b = 2.18e32 ; in python : b = 2.18e32 Type conversions are implicit in Python: in c : float c = (float) a + b in python : c = a+b In Python (v3) numbers occupy variable memory space. In c : sizeof(int) In python : sys.getsizeof(a) Course http://fr.wikibooks.org/wiki/Programmation_Python/Type Lib. doc. https://docs.python.org/3/library/stdtypes.html M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 9 Cours 01 : Introduction Types: 'integers' in python3 >>> import sys >>> a=1 >>> sys.getsizeof(a) 28 >>> a=10**10 >>> sys.getsizeof(a) 32 >>> a=10**100 >>> sys.getsizeof(a) 72 >>> a=10**1000 >>> sys.getsizeof(a) 468 >>> a=10**10000 >>> sys.getsizeof(a) 4456 >>> a=10**100000 >>> sys.getsizeof(a) 44320 >>> a=2**100000 >>> sys.getsizeof(a) 13360 >>> a = 2**1000 >>> sys.getsizeof(a) 160 >>> print(a) 1071508607186267320948425049060001810561404811705533607443750388370351051124936122493198378815695858127594672917553146 >>> NB: to interrupt an execution in console mode (including non-python): Ctrl + c M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 10 Cours 01 : Introduction Digital operators x+y sum of x and y Growing priorities x-y difference of x and y | x*y product of x and y \/ x/y quotient of x and y x // y floored quotient of x and y x%y remainder of x / y -x x negated abs(x) absolute value or magnitude of x int(x) x converted to integer float(x) x converted to floating point complex(re, im) a complex number c.conjugate() conjugate of the complex number c divmod(x, y) the pair (x // y, x % y) pow(x, y) x to the power y x ** y x to the power y Cf : https://docs.python.org/3.4/library/stdtypes.html M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 11 Cours 01 : Introduction Types: 'real' ones in python3 >>> import sys >>> a = 0.1 >>> sys.getsizeof(a) 24 NB: In 'classic' programming, 'floats' are used to represent >>> a = 2.2**2 >>> print(a) numbers. real, but their representation in memory is 4.840000000000001 limited (constraint on memory space, register size, etc.). >>> a = 2.0**2 >>> type(a) So, the precision is limited and to be taken care of by the developer (pseudo-real). >>> a = 2.0**10 >>> sys.getsizeof(a) 24 >>> a = 2.0**100 In the rest of the teaching, we will study a symbolic >>> sys.getsizeof(a) 24 calculation library which allows us to circumvent this >>> a = 2.0**1000 limitation. >>> sys.getsizeof(a) 24 >>> print(a) 1.0715086071862673e+301 >>> a=0.12345678901234567890123456789 >>> print(a) 0.12345678901234568 >> a=0.00012345678901234567890123456789 >>> print(a) 0.00012345678901234567 Float in python: between -1,7×10**308 et 1,7×10**308, 17 e = 3e309 >>> print(e) significant figures, the powers between -308 et +308 → Contrairement inf to other languages, after exceeding -> infinity... M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 12 Cours 01 : Introduction Types: Booleans Basic Operations: >>> a = True >>> b = False >>> P = (a or b) >>> type(a) >>> type(P) >>> print(P) True Resulting from a test: >>> A = 12 >>> P = (A < 13) >>> type(P) >>> print(P) True M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 13 Cours 01 : Introduction Boolean operators (return a bool) P and Q of type bool: (P and Q) → and logical (P or Q) → or logique not P → negation E1 and E2 are of any type: (E1 == E2) → equality (E of any type) (E1 is E2) → equality (value and type) (E1 != E2) → difference (E1 is not E2) → difference (3.0 is different than 3) (E1 < E2) → lower (E1 E2) → superior (E1 >= E2) → superior or equality M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 14 Cours 01 : Introduction Tests / control structures: if bool Reminder on bool algebra: print( 2 == 2 ) print( 2 == 3 ) Example of if check: if (a > b ) : ← statement ended with: print(a) ← don't tab at the beginning else : b = b-1 ← it is possible to put several instructions print(b) If check example 2: P = ( A < B) if (P) : A = B - A else : A = A -B print(A) ← one less tab indicates the end of the if/else M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 15 Cours 01 : Introduction Tests / control structures: if bool Reminder on bool algebra: print( 2 == 2 ) print( 2 == 3 ) If (a > b) Example of if check: yes no if (a > b ) : print(a) else : print(a) b=b-1 b = b-1 print(b) print(b) If check example 2: P = ( A < B) if (P) : A = B - A else : A = A -B print(A) M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 16 Cours 01 : Introduction Tests / control structures: if bool: Example of if elif else check: A = 12 B = 13 if (A < B) : A = B - A elif (A > B) : A = A -B else : A = 0 print(A) ← one less tab indicates the end of the test Example of nested control structures: a = (A * B) – 3*A if ( a > 50) : print( a , " is greater than 50 ") if (a%6 == 0): print(" the number ", a, " is divisible by 6.") else : print("le nb ", a , " is not divisible by 6.") elif (a < 50) : print(a , " is less than 50 ") M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 17 Cours 01 : Introduction Types: strings in py. A string is a sequence of any characters delimited by two ' (apostrphs) or two '' (quotation marks). Examples: Basic Operators/Functions string of characters : >>> A = "Bonjour" >>> B = 'Bonjour' str + str concatenation of 2 strings >>> type(A) float(str) convert to float >>> A is B int(str) convert to integer True str(obj.) convert in string. >>> A + " " + B 'Bonjour Bonjour' There are also many methods for cutting, deleting >>> len(A) spaces, capitalizing, lower case, test the presence of a 7 >>> A letter or a string, test if the string is composed of 'B' numbers etc. (in a subsequent lesson). >>> A 'o' >>> A[1:3] 'on' >>> A[0:len(A)] 'Bonjour' M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 18 Cours 01 : Introduction Inputs/outputs All interactions with the 'console' to display or read user input are done through character strings Example : A = 12 B = 'toto' print(A,B) # A is implicitly converted print(B, " a ", A, "ans") print(" Indicate your age ") A = input() print(" Indicate your first name ") B = input() print(B, " a ", A, "ans") # the separated arguments # are converted C = B + " a "+ A + " ans." # explicit conversion print(C) M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 19 Cours 01 : Introduction Example script In an example.py file #!/bin/python # -*- coding: utf-8 -*- """ IPSA - Info 12 2014/2015 Types and variables """ print("IPSA IN14") print(" enter 2 numbers ") a = input() b = input() a = float(a) b = float(b) c = (a + b) / 2 print(" The average of ",a," and ",b," is ",c) M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 20 Cours 01 : Introduction Example script In an example.py file #Example program print("IPSA IN14") print(" enter 2 numbers ") a = input() b = input() a = float(a) b = float(b) c = (a + b) / 2 print(" The average of ", a,« and ", b," is",c ) C = " The average of "+ str( a) + « and "+ str(b)+" is"+ str(c) print(C) print(" The average of %f and %f is %f" % (a,b,c)) print(" The average of %d and %d is %d" % (a,b,c)) print(" The average of %0.2f and %0.2f is %0.2f" % (a,b,c)) M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 21 Cours 01 : Introduction Sources & ressources Online course materials: Docs I.P.S.A. https://docs.ipsa.fr/ In 14 - Introduction to programming (with Python) IonisX → course and Ex. https://ionisx.com/courses → Python for the scientifics M. BONNEFOI I.P.S.A. - INFORMATIQUE – In 111 22 Cours 01 : Introduction

Use Quizgecko on...
Browser
Browser