SlideShare a Scribd company logo
1 of 13
Download to read offline
welcome (back) to python!
Basics: data types (and operations & calculations)
… back in 2015 …
---- file contents ----
from __future__ import division
# the first line fixes the “division” problem in python 2
# this is a comment. Comments start with “#”, and are not interpreted!
name = “Marc” # comments can also go after stuff
print “Hello”, name # print multiple things in one line with “,”
# let's get some user input
a = int(raw_input(“Write a number, any number:”))
# some basic math
b = 10 * a
# and finally, printing the result
print a, “multiplied by 10 equals:”, b
The plan!
Basics: data types (and operations & calculations)
Basics: conditionals & iteration
Basics: lists, tuples, dictionaries
Basics: writing functions
Reading & writing files: opening, parsing & formats
Working with numbers: numpy & scipy
Making plots: matplotlib & pylab
… if you guys want more after all of that …
Writing better code: functions, pep8, classes
Working with numbers: Numpy & Scipy (advanced)
Other modules: Pandas & Scikit-learn
Interactive notebooks: ipython & jupyter
Advanced topics: virtual environments & version control
The notion of “data type” is very important:
- is 1.0 the same as “1.0” ?
- is “1.0” the same as ‘1.0000’ ?
- is 1 the same as 1.0 ?
integers, floats, strings & booleans
---- file contents ----
from __future__ import division
a = 12
b = 2.5 # “decimal numbers” are called “floating point” in python
c = a + b
a = 3 # we can re-assign variables
d = c / a # remember the first line makes division work as expected
e = (d + c) % a # The modulus ‘%’ is the remainder of division
print a, b, c, d, e
# variable names don’t need to be letters
one_result = (a + d) ** c
print “The first result is:”, result_one
# but there are limits to variable names
2_result = (a - b) ** c
print “The next result is:”, 2_result
oh no = (a + c) / (b +d)
print “The next result is:”, oh no
Numbers: integers & floats
… no spaces
… cannot begin with number: 1, 2, 3 …
… cannot contain ?$@# (except _)
… cannot be a “built-in” name: and, or, int,
float, str, char, exec, file, open, object, print,
quit… (approx. 80 reserved names)
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
---- file contents ----
word = “Hello”
letter = ‘X’ # single quotes are OK too!
sentence = “Welcome to strings!”
# notice spaces in the print below?
print word, letter, sentence
# The ‘+’ and ‘*’ operators also have functions on strings
cat = word + letter
print cat
dog = word * 5
print dog
Characters, words & sentences: Strings
+ concatenate
* copy
---- file contents ----
first = raw_input(“What is your first name?”)
last = raw_input(“What is your last name?”)
full = first + last
print full
a = raw_input(“Please enter a number:”)
b = raw_input(“Please enter another one:”)
c = a + b
print c
# converting a string to an integer
a = raw_input(“Please enter a number:”)
b = raw_input(“Please enter another one:”)
c = int(a) + int(b)
print c
Converting between types
int() convert to an integer
str() convert to a string
float() convert to a float
Converting between types
---- file contents ----
a = raw_input(“Number: ”)
b = raw_input(“Another one number: ”)
c = int(a) + int(b)
print “Result:”, c
a = int(raw_input(“Number: ”))
b = int(raw_input(“Another number: ”))
c = a + b
print “Result:”, c
a = raw_input(“Number: ”)
b = raw_input(“Another one number: ”)
c = int(a) + int(b)
print int(“Result:”, c)
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
print “Result:”, int(a) + int(b)
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
c = int(a + b)
print “Result:”, c
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
c = int(a) + int(b)
print “Result:”, str(c)
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
c = str(int(a) + int(b))
print “Result:”, c
---- file contents ----
a = raw_input(“Please enter a number”)
b = raw_input(“Please enter another one”)
print “a is type:”, type(a)
print “b is type:”, type(b)
c = int(a) + int(b)
print “c is type:”, type(c)
print c
Converting between types
int() convert to an integer
str() convert to a string
float() convert to a float
type() determine the type of a variable
---- file contents ----
a = 5
b = 10
c = 5
# testing for equality
print “Does a equal b?”, a == b
print “Does a equal c?”, a == c
# tests result in boolean variables
x = (a == b) # the parenthesis are not really needed
print “Does a equal b?”, x
print “The type of x is:”, type(x)
print “Is a larger than 5?”, x > 5
# tests are (almost all) type specific!
print 1.0 == “1.0”
print “1.0” == “1.0000”
print 1 == 1.0
True or False: Boolean
= = equal
! = not equal
> greater than
>= greater or equal than
< less than
<= less or equal than
… python is “smart” enough to realize
that 1 and 1.0 (as integers and float) are
the same number.
- is 1.0 the same as “1.0” ?
- is “1.0” the same as ‘1.0000’ ?
- is 1 the same as 1.0 ?
Some quick tests…
type('3.14')
<type ‘str’>
type(3.14)
<type ‘float’>
type(3)
<type ‘int’>
type(True)
<type ‘bool’>
type(3>1)
<type ‘bool’>
float('3.14')
3.14 (float)
int('3')
3 (int)
int('Python')
err
float('Python')
err
int(3.9)
3 (int)
int(‘3.9’)
err
float(3)
3.0 (float)
float(-3.14)
-3.14 (float)
str(-3.14)
“-3.14” (str)
str(Python)
err
str(x = 0)
err
_x = 0
_x, int value 0
1x = 0
err
-x = 0
err
x + y = 0
err
x = 0.0
x, float 0.0
x = ## 0.0
err
0 == 0.0
True (bool)
'0' == '0.0'
False (bool)
x = '0'
x, str value ‘0’
x = '0' + '0'
x, str value ‘00’
x = 2 * '0'
x, str value ‘00’
y = 'x' + 'x'
y, str value ‘xx’
y = '2' + '1'
y, str value ‘21’
y = '2' - '1'
err
Mashing it all together… exercices!
- Write a script that prompts a user for 3 numbers, and prints the following values
to the screen:
- The sum of the three
- The average of the three
- The maximum of the three (google some if you need to)
cheatsheet = = equal
! = not equal
> greater than
>= greater or equal than
< less than
<= less or equal than
int() convert to an integer
str() convert to a string
float() convert to a float
type() determine the type of a variable
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
+ concatenate
* copy
raw_input(“text”) prompt user input (string)
print “some”, 1, 2, “values” raw_input(“text”) prints (any) values to the screen
strings
type conversion
conditions / equality
numbers
(user) input and output

More Related Content

What's hot

Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fuclimatewarrior
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Paige Bailey
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanWei-Yuan Chang
 
Python Traning presentation
Python Traning presentationPython Traning presentation
Python Traning presentationNimrita Koul
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, pythonRobert Lujo
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data StructureChan Shik Lim
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기진성 오
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming LanguageRohan Gupta
 
Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기JangHyuk You
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxEleanor McHugh
 
Python basic
Python basic Python basic
Python basic sewoo lee
 
Python легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиMaxim Kulsha
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeKevlin Henney
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...Matt Harrison
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Matt Harrison
 

What's hot (20)

Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Python Traning presentation
Python Traning presentationPython Traning presentation
Python Traning presentation
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
Python- strings
Python- stringsPython- strings
Python- strings
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기
 
F# intro
F# introF# intro
F# intro
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
 
Python basic
Python basic Python basic
Python basic
 
Python легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачи
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
 
Python
PythonPython
Python
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 

Viewers also liked

Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: FunctionsMarc Gouw
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programmingMarc Gouw
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionariesMarc Gouw
 
Class 4: For and while
Class 4: For and whileClass 4: For and while
Class 4: For and whileMarc Gouw
 
Class 8a: Modules and imports
Class 8a: Modules and importsClass 8a: Modules and imports
Class 8a: Modules and importsMarc Gouw
 
Class 7b: Files & File I/O
Class 7b: Files & File I/OClass 7b: Files & File I/O
Class 7b: Files & File I/OMarc Gouw
 
RESUME Jitendra Dixit - Copy
RESUME Jitendra Dixit - CopyRESUME Jitendra Dixit - Copy
RESUME Jitendra Dixit - CopyJitendra Dixit
 
문플 경진대회
문플 경진대회문플 경진대회
문플 경진대회승주 한
 

Viewers also liked (13)

Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
 
HDPE Pipe
HDPE PipeHDPE Pipe
HDPE Pipe
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programming
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionaries
 
Class 4: For and while
Class 4: For and whileClass 4: For and while
Class 4: For and while
 
Class 8a: Modules and imports
Class 8a: Modules and importsClass 8a: Modules and imports
Class 8a: Modules and imports
 
Intelligenza creativa
Intelligenza creativaIntelligenza creativa
Intelligenza creativa
 
Class 7b: Files & File I/O
Class 7b: Files & File I/OClass 7b: Files & File I/O
Class 7b: Files & File I/O
 
Heimo&Jolkkonen
Heimo&JolkkonenHeimo&Jolkkonen
Heimo&Jolkkonen
 
PR for FITBIT
PR for FITBITPR for FITBIT
PR for FITBIT
 
RESUME Jitendra Dixit - Copy
RESUME Jitendra Dixit - CopyRESUME Jitendra Dixit - Copy
RESUME Jitendra Dixit - Copy
 
문플 경진대회
문플 경진대회문플 경진대회
문플 경진대회
 
Renaissance
RenaissanceRenaissance
Renaissance
 

Similar to Python basics: data types, operations, conversions and calculations

Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/elseMarc Gouw
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshopBAINIDA
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine LearningYounesCharfaoui
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schoolsDan Bowen
 
Basic NLP with Python and NLTK
Basic NLP with Python and NLTKBasic NLP with Python and NLTK
Basic NLP with Python and NLTKFrancesco Bruni
 
Crystal presentation in NY
Crystal presentation in NYCrystal presentation in NY
Crystal presentation in NYCrystal Language
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computingGo Asgard
 
An Intro to Python in 30 minutes
An Intro to Python in 30 minutesAn Intro to Python in 30 minutes
An Intro to Python in 30 minutesSumit Raj
 
Chapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxChapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxSovannDoeur
 

Similar to Python basics: data types, operations, conversions and calculations (20)

Python crush course
Python crush coursePython crush course
Python crush course
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Python
PythonPython
Python
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/else
 
python codes
python codespython codes
python codes
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
C tutorial
C tutorialC tutorial
C tutorial
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine Learning
 
Python slide
Python slidePython slide
Python slide
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Basic NLP with Python and NLTK
Basic NLP with Python and NLTKBasic NLP with Python and NLTK
Basic NLP with Python and NLTK
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Crystal presentation in NY
Crystal presentation in NYCrystal presentation in NY
Crystal presentation in NY
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
 
An Intro to Python in 30 minutes
An Intro to Python in 30 minutesAn Intro to Python in 30 minutes
An Intro to Python in 30 minutes
 
C tutorial
C tutorialC tutorial
C tutorial
 
Chapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxChapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptx
 

Recently uploaded

Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫qfactory1
 
Cytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxCytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxVarshiniMK
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.PraveenaKalaiselvan1
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Masticationvidulajaib
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Module 4: Mendelian Genetics and Punnett Square
Module 4:  Mendelian Genetics and Punnett SquareModule 4:  Mendelian Genetics and Punnett Square
Module 4: Mendelian Genetics and Punnett SquareIsiahStephanRadaza
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxpriyankatabhane
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaDashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaPraksha3
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2John Carlo Rollon
 
TOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxTOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxdharshini369nike
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)DHURKADEVIBASKAR
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxNandakishor Bhaurao Deshmukh
 

Recently uploaded (20)

Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫
 
Cytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptxCytokinin, mechanism and its application.pptx
Cytokinin, mechanism and its application.pptx
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
 
Temporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of MasticationTemporomandibular joint Muscles of Mastication
Temporomandibular joint Muscles of Mastication
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Module 4: Mendelian Genetics and Punnett Square
Module 4:  Mendelian Genetics and Punnett SquareModule 4:  Mendelian Genetics and Punnett Square
Module 4: Mendelian Genetics and Punnett Square
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaDashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2
 
TOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxTOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptx
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
 

Python basics: data types, operations, conversions and calculations

  • 1. welcome (back) to python! Basics: data types (and operations & calculations)
  • 2. … back in 2015 … ---- file contents ---- from __future__ import division # the first line fixes the “division” problem in python 2 # this is a comment. Comments start with “#”, and are not interpreted! name = “Marc” # comments can also go after stuff print “Hello”, name # print multiple things in one line with “,” # let's get some user input a = int(raw_input(“Write a number, any number:”)) # some basic math b = 10 * a # and finally, printing the result print a, “multiplied by 10 equals:”, b
  • 3. The plan! Basics: data types (and operations & calculations) Basics: conditionals & iteration Basics: lists, tuples, dictionaries Basics: writing functions Reading & writing files: opening, parsing & formats Working with numbers: numpy & scipy Making plots: matplotlib & pylab … if you guys want more after all of that … Writing better code: functions, pep8, classes Working with numbers: Numpy & Scipy (advanced) Other modules: Pandas & Scikit-learn Interactive notebooks: ipython & jupyter Advanced topics: virtual environments & version control
  • 4. The notion of “data type” is very important: - is 1.0 the same as “1.0” ? - is “1.0” the same as ‘1.0000’ ? - is 1 the same as 1.0 ? integers, floats, strings & booleans
  • 5. ---- file contents ---- from __future__ import division a = 12 b = 2.5 # “decimal numbers” are called “floating point” in python c = a + b a = 3 # we can re-assign variables d = c / a # remember the first line makes division work as expected e = (d + c) % a # The modulus ‘%’ is the remainder of division print a, b, c, d, e # variable names don’t need to be letters one_result = (a + d) ** c print “The first result is:”, result_one # but there are limits to variable names 2_result = (a - b) ** c print “The next result is:”, 2_result oh no = (a + c) / (b +d) print “The next result is:”, oh no Numbers: integers & floats … no spaces … cannot begin with number: 1, 2, 3 … … cannot contain ?$@# (except _) … cannot be a “built-in” name: and, or, int, float, str, char, exec, file, open, object, print, quit… (approx. 80 reserved names) + addition - subtraction * multiplication / division ** exponent % modulus
  • 6. ---- file contents ---- word = “Hello” letter = ‘X’ # single quotes are OK too! sentence = “Welcome to strings!” # notice spaces in the print below? print word, letter, sentence # The ‘+’ and ‘*’ operators also have functions on strings cat = word + letter print cat dog = word * 5 print dog Characters, words & sentences: Strings + concatenate * copy
  • 7. ---- file contents ---- first = raw_input(“What is your first name?”) last = raw_input(“What is your last name?”) full = first + last print full a = raw_input(“Please enter a number:”) b = raw_input(“Please enter another one:”) c = a + b print c # converting a string to an integer a = raw_input(“Please enter a number:”) b = raw_input(“Please enter another one:”) c = int(a) + int(b) print c Converting between types int() convert to an integer str() convert to a string float() convert to a float
  • 8. Converting between types ---- file contents ---- a = raw_input(“Number: ”) b = raw_input(“Another one number: ”) c = int(a) + int(b) print “Result:”, c a = int(raw_input(“Number: ”)) b = int(raw_input(“Another number: ”)) c = a + b print “Result:”, c a = raw_input(“Number: ”) b = raw_input(“Another one number: ”) c = int(a) + int(b) print int(“Result:”, c) a = raw_input(“Number: ”) b = raw_input(“Another number: ”) print “Result:”, int(a) + int(b) a = raw_input(“Number: ”) b = raw_input(“Another number: ”) c = int(a + b) print “Result:”, c a = raw_input(“Number: ”) b = raw_input(“Another number: ”) c = int(a) + int(b) print “Result:”, str(c) a = raw_input(“Number: ”) b = raw_input(“Another number: ”) c = str(int(a) + int(b)) print “Result:”, c
  • 9. ---- file contents ---- a = raw_input(“Please enter a number”) b = raw_input(“Please enter another one”) print “a is type:”, type(a) print “b is type:”, type(b) c = int(a) + int(b) print “c is type:”, type(c) print c Converting between types int() convert to an integer str() convert to a string float() convert to a float type() determine the type of a variable
  • 10. ---- file contents ---- a = 5 b = 10 c = 5 # testing for equality print “Does a equal b?”, a == b print “Does a equal c?”, a == c # tests result in boolean variables x = (a == b) # the parenthesis are not really needed print “Does a equal b?”, x print “The type of x is:”, type(x) print “Is a larger than 5?”, x > 5 # tests are (almost all) type specific! print 1.0 == “1.0” print “1.0” == “1.0000” print 1 == 1.0 True or False: Boolean = = equal ! = not equal > greater than >= greater or equal than < less than <= less or equal than … python is “smart” enough to realize that 1 and 1.0 (as integers and float) are the same number. - is 1.0 the same as “1.0” ? - is “1.0” the same as ‘1.0000’ ? - is 1 the same as 1.0 ?
  • 11. Some quick tests… type('3.14') <type ‘str’> type(3.14) <type ‘float’> type(3) <type ‘int’> type(True) <type ‘bool’> type(3>1) <type ‘bool’> float('3.14') 3.14 (float) int('3') 3 (int) int('Python') err float('Python') err int(3.9) 3 (int) int(‘3.9’) err float(3) 3.0 (float) float(-3.14) -3.14 (float) str(-3.14) “-3.14” (str) str(Python) err str(x = 0) err _x = 0 _x, int value 0 1x = 0 err -x = 0 err x + y = 0 err x = 0.0 x, float 0.0 x = ## 0.0 err 0 == 0.0 True (bool) '0' == '0.0' False (bool) x = '0' x, str value ‘0’ x = '0' + '0' x, str value ‘00’ x = 2 * '0' x, str value ‘00’ y = 'x' + 'x' y, str value ‘xx’ y = '2' + '1' y, str value ‘21’ y = '2' - '1' err
  • 12. Mashing it all together… exercices! - Write a script that prompts a user for 3 numbers, and prints the following values to the screen: - The sum of the three - The average of the three - The maximum of the three (google some if you need to)
  • 13. cheatsheet = = equal ! = not equal > greater than >= greater or equal than < less than <= less or equal than int() convert to an integer str() convert to a string float() convert to a float type() determine the type of a variable + addition - subtraction * multiplication / division ** exponent % modulus + concatenate * copy raw_input(“text”) prompt user input (string) print “some”, 1, 2, “values” raw_input(“text”) prints (any) values to the screen strings type conversion conditions / equality numbers (user) input and output