Welcometo Python Course Basic
Naiyan Noor
BSc inCSE
BangladeshArmyUniversityofScienceandTechnology-BAUST
A snake.
A British comedy group called MontyPython.
A programming languge. Thedefinition of the language:words, punctuation (operators)
and grammar(syntax).
Thecompiler/interpreterof the Pythonprogramminglanguage. (aka. CPython).
What is Python?
Aneditor wherewe canwrite ina language.
A compiler or interpreterthat can translate our text to the language of the
computer.
What is needed to write a program?
Python2.x -old, legacy code at companies, answers onthe Internet. Retires on
January1,2020.
Python3.x -the one that youshould use. (not fully backward compatible)
Available since December 3, 2008.
Python 2 vs. Python 3.
Microsoft Windows
Installation
Linux
MacOS
We aregoingtocover howtoinstallPythonall3majoroperatingsystems.
Anaconda with Python3.x
Anaconda shell
Anaconda Jupyternotebook
Installation on Microsoft Windows
Basically youcan use anytext editor to write Pythoncode. TheminimumI
recommendis to haveproper syntax highlighting. IDEs will also provide
intellisense, that is, inmost of the cases they will be able to understand what
kindof objects do youhavein yourcode and will beable to show youthe
available methods and theirparameters. Even better, theyprovide powerful
debuggers.
Editors, IDEs
Emacs
vi,vim,gvim
spf13-vim
Kate
Gedit
jEdit”
Editors IDE Linux ,Windows or Mac
Notepad++
Textpad
UltraEdit
Linux Windows Mac
CotEditor
TextWrangler
TextMate
Type“texteditor”inyour
AppleStore(filtertofree)
PyCharmcommunity edition
Visual Codeof Microsoft
Spyder, a scientific environment (included in Anaconda)
Jupyter with IPython behind the scene.
IDLE(comes with Python)
KomodoofActiveState
Aptana
Pyscripter
PyDev(for Eclipse)
Wing IDE
IDEs
More or less theonly thing I do on thecommand line with pythonis to checkthe
version number:
Python on the command line
1 python -V
2 python --version
Youcan runsome Pythoncode without creating a file, but I don’t rememeber
ever needing this. If you insists
Python on the command line
1python-c "print 42"
1 python3 -c"print(42)"
Typethe following to get thedetails:
1man python
cmdline
Createa file called hello.py with the above content.
Open yourterminal or the Anaconda Prompt on MS Windows inthe directory
(folder)
Changeto thedirectory whereyousaved the file.
Runit by typing python hello.py or python3hello.py
Theextension is .py-mostly for the editor (but also for modules).
Parentheses after print() are required in Python3, but usethem evenif youare
stuck onPython 2.
First script - hello world
1print("hello")
2
3 # Comments for other developers
4
5print("world") # morecomments
6
7 # print("Thisis not printed")
Comments
1 greeting = "Hello World!"
2 print(greeting)”
Variables
Exercise: Hello world)
Tryyourenvironment:
Makesure you haveaccess to the right version of Python.
Install Pythonif needed.
What is programming?
Use some language totell the computer what todo.
Like a cooking recepie it has step-by-stepinstructions.
Taking acomplex problemand dividing it into small steps acomputer can
do.
What are the programming languages
A computer CPUis createdfrom transistors, 1and 0 values. (aka. bits)
Its language consists of numbers. (e.g 37 means move the content of ax
register to bxregister)
English? too complex, toomuch ambiguity.
Programming languages are in-beteen.
A programming language
Built-in words: print, len, type,def,…
Literal values: numbers, strings
Operators:+ - * = , ; …
Grammar (syntax)
User-createdwords: variables, functions, classes, ….
Words and punctuation matter!
What did you chose? (Correctly:choose, but people will usually
understand.)
Lets do the homework. (Correctly:Let’s, but most people will understand.)
Let’s eat, grandpa!
Let’s eat grandpa!
Words and punctuation matter!
Whatdid you chose? (Correctly:choose, butpeoplewill usuallyunderstand.)
Letsdo thehomework.(Correctly:Let’s,butmostpeoplewill understand.)
Let’seat,grandpa!
Let’seatgrandpa!
Programminglanguageshavea lotlesswords,buttheyarevery stricton thegrammar(syntax).
Amising comma can breakyour code.
Amissing space willchange themeaningof your code.
Anincorreectwordcan ruinyour day.
Literals, Value Types in Python
“ 1print(type(23) ) #int
2 print(type(3.14) ) #float
3 print(type("hello")) #str
4
5 print(type("23") ) #str
6 print(type("3.24") ) #str
7
8 print(type(None)) #NoneType
9 print(type(True)) #bool
10 print(type(False) ) #bool
11
12 print(type([])) # list
13 print(type({})) # dict
14
15 print(type(hello)) # NameError:name'hello' is notdefined
16 print("Stillrunning")
Multiply string
1width= "23"
2height= "17"
3area= width*height
4print(area)
1Traceback(most recentcall last):
2 File"python/examples/basics/rectangular_strings.py",line3, in<module>
3 area=width*height
4 TypeError:can't multiplysequence by non-intof type 'str”
Add numbers
“1a =19
2b= 23
3c =a + b
4print(c) #42”
Add numbers & Add strings
“1a = 19
2 b = 23
3 c = a + b
4 print(c) # 42”
1 a = "19"
2 b = "23"
3 c = a + b
4 print(c) # 1923
Numbers Strings
Exercise: Calculations
“Extend the rectangular_basic.py from above to print both the area and the
circumferenceof the rectangle.
Write a script that has a variable holding the radius of a circleand prints out the area
of the circleand the circumferenceof the circle.
Write a script that has two numbers a and b and prints out the results of a+b, a-b,
a*b, a/b”
Solution: Calculations
1a =19
2b = 23
3c= a + b
4prin“1width = 23
2height= 17
3area = width *height
4print("The area is", area) # 391
5circumference = 2*(width + height)
6print("The circumference is", circumference) # 80
1r= 7
2pi= 3.14
3print("The area is", r* r* pi) # 153.86
4print("The circumference is", 2* r* pi) # 43.96
Solution: Calculations
1importmath
2
3r= 7
4print("The area is", r* r* math.pi) # 153.9380400258998
5print("The circumference is", 2* r* math.pi) # 43.982297150257104
1a =3
2b = 2
3
4print(a+b) # 5
5print(a-b) # 1
“6print(a*b) # 6
7print(a/b) # 1.5”
Solution: Calculations
1importmath
2
3r= 7
4print("The area is", r* r* math.pi) # 153.9380400258998
5print("The circumference is", 2* r* math.pi) # 43.982297150257104
1a =3
2b = 2
3
4print(a+b) # 5
5print(a-b) # 1
“6print(a*b) # 6
7print(a/b) # 1.5”
Thank You.

Python Course Basic

  • 1.
    Welcometo Python CourseBasic Naiyan Noor BSc inCSE BangladeshArmyUniversityofScienceandTechnology-BAUST
  • 2.
    A snake. A Britishcomedy group called MontyPython. A programming languge. Thedefinition of the language:words, punctuation (operators) and grammar(syntax). Thecompiler/interpreterof the Pythonprogramminglanguage. (aka. CPython). What is Python?
  • 3.
    Aneditor wherewe canwriteina language. A compiler or interpreterthat can translate our text to the language of the computer. What is needed to write a program?
  • 4.
    Python2.x -old, legacycode at companies, answers onthe Internet. Retires on January1,2020. Python3.x -the one that youshould use. (not fully backward compatible) Available since December 3, 2008. Python 2 vs. Python 3.
  • 5.
    Microsoft Windows Installation Linux MacOS We aregoingtocoverhowtoinstallPythonall3majoroperatingsystems.
  • 6.
    Anaconda with Python3.x Anacondashell Anaconda Jupyternotebook Installation on Microsoft Windows
  • 7.
    Basically youcan useanytext editor to write Pythoncode. TheminimumI recommendis to haveproper syntax highlighting. IDEs will also provide intellisense, that is, inmost of the cases they will be able to understand what kindof objects do youhavein yourcode and will beable to show youthe available methods and theirparameters. Even better, theyprovide powerful debuggers. Editors, IDEs
  • 8.
    Emacs vi,vim,gvim spf13-vim Kate Gedit jEdit” Editors IDE Linux,Windows or Mac Notepad++ Textpad UltraEdit Linux Windows Mac CotEditor TextWrangler TextMate Type“texteditor”inyour AppleStore(filtertofree)
  • 9.
    PyCharmcommunity edition Visual CodeofMicrosoft Spyder, a scientific environment (included in Anaconda) Jupyter with IPython behind the scene. IDLE(comes with Python) KomodoofActiveState Aptana Pyscripter PyDev(for Eclipse) Wing IDE IDEs
  • 10.
    More or lesstheonly thing I do on thecommand line with pythonis to checkthe version number: Python on the command line 1 python -V 2 python --version
  • 11.
    Youcan runsome Pythoncodewithout creating a file, but I don’t rememeber ever needing this. If you insists Python on the command line 1python-c "print 42" 1 python3 -c"print(42)" Typethe following to get thedetails: 1man python cmdline
  • 12.
    Createa file calledhello.py with the above content. Open yourterminal or the Anaconda Prompt on MS Windows inthe directory (folder) Changeto thedirectory whereyousaved the file. Runit by typing python hello.py or python3hello.py Theextension is .py-mostly for the editor (but also for modules). Parentheses after print() are required in Python3, but usethem evenif youare stuck onPython 2. First script - hello world
  • 13.
    1print("hello") 2 3 # Commentsfor other developers 4 5print("world") # morecomments 6 7 # print("Thisis not printed") Comments
  • 14.
    1 greeting ="Hello World!" 2 print(greeting)” Variables Exercise: Hello world) Tryyourenvironment: Makesure you haveaccess to the right version of Python. Install Pythonif needed.
  • 15.
    What is programming? Usesome language totell the computer what todo. Like a cooking recepie it has step-by-stepinstructions. Taking acomplex problemand dividing it into small steps acomputer can do.
  • 16.
    What are theprogramming languages A computer CPUis createdfrom transistors, 1and 0 values. (aka. bits) Its language consists of numbers. (e.g 37 means move the content of ax register to bxregister) English? too complex, toomuch ambiguity. Programming languages are in-beteen.
  • 17.
    A programming language Built-inwords: print, len, type,def,… Literal values: numbers, strings Operators:+ - * = , ; … Grammar (syntax) User-createdwords: variables, functions, classes, ….
  • 18.
    Words and punctuationmatter! What did you chose? (Correctly:choose, but people will usually understand.) Lets do the homework. (Correctly:Let’s, but most people will understand.) Let’s eat, grandpa! Let’s eat grandpa!
  • 19.
    Words and punctuationmatter! Whatdid you chose? (Correctly:choose, butpeoplewill usuallyunderstand.) Letsdo thehomework.(Correctly:Let’s,butmostpeoplewill understand.) Let’seat,grandpa! Let’seatgrandpa! Programminglanguageshavea lotlesswords,buttheyarevery stricton thegrammar(syntax). Amising comma can breakyour code. Amissing space willchange themeaningof your code. Anincorreectwordcan ruinyour day.
  • 20.
    Literals, Value Typesin Python “ 1print(type(23) ) #int 2 print(type(3.14) ) #float 3 print(type("hello")) #str 4 5 print(type("23") ) #str 6 print(type("3.24") ) #str 7 8 print(type(None)) #NoneType 9 print(type(True)) #bool 10 print(type(False) ) #bool 11 12 print(type([])) # list 13 print(type({})) # dict 14 15 print(type(hello)) # NameError:name'hello' is notdefined 16 print("Stillrunning")
  • 21.
    Multiply string 1width= "23" 2height="17" 3area= width*height 4print(area) 1Traceback(most recentcall last): 2 File"python/examples/basics/rectangular_strings.py",line3, in<module> 3 area=width*height 4 TypeError:can't multiplysequence by non-intof type 'str”
  • 22.
    Add numbers “1a =19 2b=23 3c =a + b 4print(c) #42”
  • 23.
    Add numbers &Add strings “1a = 19 2 b = 23 3 c = a + b 4 print(c) # 42” 1 a = "19" 2 b = "23" 3 c = a + b 4 print(c) # 1923 Numbers Strings Exercise: Calculations “Extend the rectangular_basic.py from above to print both the area and the circumferenceof the rectangle. Write a script that has a variable holding the radius of a circleand prints out the area of the circleand the circumferenceof the circle. Write a script that has two numbers a and b and prints out the results of a+b, a-b, a*b, a/b”
  • 24.
    Solution: Calculations 1a =19 2b= 23 3c= a + b 4prin“1width = 23 2height= 17 3area = width *height 4print("The area is", area) # 391 5circumference = 2*(width + height) 6print("The circumference is", circumference) # 80 1r= 7 2pi= 3.14 3print("The area is", r* r* pi) # 153.86 4print("The circumference is", 2* r* pi) # 43.96
  • 25.
    Solution: Calculations 1importmath 2 3r= 7 4print("Thearea is", r* r* math.pi) # 153.9380400258998 5print("The circumference is", 2* r* math.pi) # 43.982297150257104 1a =3 2b = 2 3 4print(a+b) # 5 5print(a-b) # 1 “6print(a*b) # 6 7print(a/b) # 1.5”
  • 26.
    Solution: Calculations 1importmath 2 3r= 7 4print("Thearea is", r* r* math.pi) # 153.9380400258998 5print("The circumference is", 2* r* math.pi) # 43.982297150257104 1a =3 2b = 2 3 4print(a+b) # 5 5print(a-b) # 1 “6print(a*b) # 6 7print(a/b) # 1.5”
  • 27.