SlideShare a Scribd company logo
1 of 8
High-level Programming with Python
Expressions and Variables
Alex Ropelewski
PSC-NRBSC
Bienvenido Vélez
UPR Mayaguez
Reference: How to Think Like a Computer Scientist: Learning with Python
Essential Computing for Bioinformatics
1
Integer Expressions
3
>>> print 1 + 3
4
>>> print 6 * 7
42
>>> print 6 * 7 + 2
44
>>> print 2 + 6 * 7
44
>>> print 6 - 2 - 3
1
>>> print 6 - ( 2 - 3)
7
>>> print 1 / 3
0
>>>
/ and * higher precedence than + and -
integer division truncates fractional part
Operators are left associative
Parenthesis can override precedence
Floating Point Expressions
4
>>> print 1.0 / 3.0
0.333333333333
>>> print 1.0 + 2
3.0
>>> print 3.3 * 4.23
13.959
>>> print 3.3e23 * 2
6.6e+023
>>> print float(1) /3
0.333333333333
>>>
Mixed operations converted to float
Scientific notation allowed
12 decimal digits default precision
Explicit conversion
String Expressions
5
>>> print "aaa"
aaa
>>> print "aaa" + "ccc"
aaaccc
>>> len("aaa")
3
>>> len ("aaa" + "ccc")
6
>>> print "aaa" * 4
aaaaaaaaaaaa
>>> "aaa"
'aaa'
>>> "c" in "atc"
True
>>> "g" in "atc"
False
>>>
+ concatenates string
len is a function that returns the length
of its argument string
any expression can be an argument
* replicates strings
a value is an expression that yields itself
in operator finds a string inside another
And returns a boolean result
Values Can Have (MEANINGFUL) Names
6
>>> numAminoAcids = 20
>>> eValue = 6.022e23
>>> prompt = "Enter a sequence ->"
>>> print numAminoAcids
20
>>> print eValue
6.022e+023
>>> print prompt
Enter a sequence ->
>>> print "prompt"
prompt
>>>
>>> prompt = 5
>>> print prompt
5
>>>
= binds a name to a value
prints the value bound to a name
= can change the value associated
with a name even to a different type
use Camel case for compound names
In Bioinformatics Words …
8
>>> codon=“atg”
>>> codon * 3
’atgatgatg’
>>> seq1 =“agcgccttgaattcggcaccaggcaaatctcaaggagaagttccggggagaaggtgaaga”
>>> seq2 = “cggggagtggggagttgagtcgcaagatgagcgagcggatgtccactatgagcgataata”
>>> seq = seq1 + seq2
>>> seq
'agcgccttgaattcggcaccaggcaaatctcaaggagaagttccggggagaaggtgaagacggggagtggggagttgagtc
gcaagatgagcgagcggatgtccactatgagcgataata‘
>>> seq[1]
'g'
>>> seq[0]
'a'
>>> “a” in seq
True
>>> len(seq1)
60
>>> len(seq)
120
First nucleotide starts at 0
More Bioinformatics
Extracting Information from Sequences
9
>>> seq[0] + seq[1] + seq[2]
’agc’
>>> seq[0:3]
’agc’
>>> seq[3:6]
’gcc’
>>> seq.count(’a’)
35
>>> seq.count(’c’)
21
>>> seq.count(’g’)
44
>>> seq.count(’t’)
12
>>> long = len(seq)
>>> pctA = seq.count(’a’)
>>> float(pctA) / long * 100
29.166666666666668
Find the first codon from the sequence
get ’slices’ from strings:
How many of each base does
this sequence contain?
Count the percentage of
each base on the sequence.
Additional Note About Python Strings
10
>>> seq=“ACGT”
>>> print seq
ACGT
>>> seq=“TATATA”
>>> print seq
TATATA
>>> seq[0] = seq[1]
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
seq[0]=seq[1]
TypeError: 'str' object does not support item assignment
seq = seq[1] + seq[1:]
Can replace
one whole
string with
another
whole string
Can NOT
simply replace
a sequence
character with
another
sequence
character, but…
Can replace a whole string using substrings

More Related Content

What's hot

Python data structures
Python data structuresPython data structures
Python data structures
Harry Potter
 

What's hot (12)

Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
 
«PyWat. А хорошо ли вы знаете Python?» Александр Швец, Marilyn System
«PyWat. А хорошо ли вы знаете Python?» Александр Швец, Marilyn System«PyWat. А хорошо ли вы знаете Python?» Александр Швец, Marilyn System
«PyWat. А хорошо ли вы знаете Python?» Александр Швец, Marilyn System
 
Project fast food automaton
Project fast food automatonProject fast food automaton
Project fast food automaton
 
第3回 データフレームの基本操作 その1(解答付き)
第3回 データフレームの基本操作 その1(解答付き)第3回 データフレームの基本操作 その1(解答付き)
第3回 データフレームの基本操作 その1(解答付き)
 
第6回 関数とフロー制御
第6回 関数とフロー制御第6回 関数とフロー制御
第6回 関数とフロー制御
 
Kisi2+met num
Kisi2+met numKisi2+met num
Kisi2+met num
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
 
第4回 データフレームの基本操作 その2(解答付き)
第4回 データフレームの基本操作 その2(解答付き)第4回 データフレームの基本操作 その2(解答付き)
第4回 データフレームの基本操作 その2(解答付き)
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法
 
Fluent Refactoring (Lone Star Ruby Conf 2013)
Fluent Refactoring (Lone Star Ruby Conf 2013)Fluent Refactoring (Lone Star Ruby Conf 2013)
Fluent Refactoring (Lone Star Ruby Conf 2013)
 
Python data structures
Python data structuresPython data structures
Python data structures
 

Similar to Expressions and Variables

الجلسة الأولى
الجلسة الأولىالجلسة الأولى
الجلسة الأولى
Yaman Rajab
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Python
pugpe
 

Similar to Expressions and Variables (20)

Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
الجلسة الأولى
الجلسة الأولىالجلسة الأولى
الجلسة الأولى
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Python 1
Python 1Python 1
Python 1
 
JavaOne 2016 -Emerging Web App Architectures using Java and node.js
JavaOne 2016 -Emerging Web App Architectures using Java and node.jsJavaOne 2016 -Emerging Web App Architectures using Java and node.js
JavaOne 2016 -Emerging Web App Architectures using Java and node.js
 
Τα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonΤα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την Python
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
 
機械学習と自動微分
機械学習と自動微分機械学習と自動微分
機械学習と自動微分
 
Welcome to python
Welcome to pythonWelcome to python
Welcome to python
 
Shallow copy and deep copy
Shallow copy and deep copyShallow copy and deep copy
Shallow copy and deep copy
 
Shallow copy and deep copy
Shallow copy and deep copyShallow copy and deep copy
Shallow copy and deep copy
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Python
 
python-cheatsheets.pdf
python-cheatsheets.pdfpython-cheatsheets.pdf
python-cheatsheets.pdf
 
python-cheatsheets that will be for coders
python-cheatsheets that will be for coderspython-cheatsheets that will be for coders
python-cheatsheets that will be for coders
 
901131 examples
901131 examples901131 examples
901131 examples
 
D crypto api
D crypto apiD crypto api
D crypto api
 
제 6회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 6회 엑셈 수요 세미나 자료 연구컨텐츠팀제 6회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 6회 엑셈 수요 세미나 자료 연구컨텐츠팀
 

More from BienvenidoVelezUPR

More from BienvenidoVelezUPR (20)

Icom4015 lecture11-s16
Icom4015 lecture11-s16Icom4015 lecture11-s16
Icom4015 lecture11-s16
 
Icom4015 lecture3-s18
Icom4015 lecture3-s18Icom4015 lecture3-s18
Icom4015 lecture3-s18
 
Icom4015 lecture4-f17
Icom4015 lecture4-f17Icom4015 lecture4-f17
Icom4015 lecture4-f17
 
Icom4015 lecture3-f17
Icom4015 lecture3-f17Icom4015 lecture3-f17
Icom4015 lecture3-f17
 
Icom4015 lecture3-f17
Icom4015 lecture3-f17Icom4015 lecture3-f17
Icom4015 lecture3-f17
 
CiIC4010-chapter-2-f17
CiIC4010-chapter-2-f17CiIC4010-chapter-2-f17
CiIC4010-chapter-2-f17
 
CIIC 4010 Chapter 1 f17
CIIC 4010 Chapter 1 f17CIIC 4010 Chapter 1 f17
CIIC 4010 Chapter 1 f17
 
Icom4015 lecture15-f16
Icom4015 lecture15-f16Icom4015 lecture15-f16
Icom4015 lecture15-f16
 
Icom4015 lecture14-f16
Icom4015 lecture14-f16Icom4015 lecture14-f16
Icom4015 lecture14-f16
 
Icom4015 lecture12-s16
Icom4015 lecture12-s16Icom4015 lecture12-s16
Icom4015 lecture12-s16
 
Icom4015 lecture13-f16
Icom4015 lecture13-f16Icom4015 lecture13-f16
Icom4015 lecture13-f16
 
Icom4015 lecture9-f16
Icom4015 lecture9-f16Icom4015 lecture9-f16
Icom4015 lecture9-f16
 
Icom4015 lecture10-f16
Icom4015 lecture10-f16Icom4015 lecture10-f16
Icom4015 lecture10-f16
 
Icom4015 lecture8-f16
Icom4015 lecture8-f16Icom4015 lecture8-f16
Icom4015 lecture8-f16
 
Icom4015 lecture7-f16
Icom4015 lecture7-f16Icom4015 lecture7-f16
Icom4015 lecture7-f16
 
Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016
 
Advanced Programming Lecture 5 Fall 2016
Advanced Programming Lecture 5 Fall 2016Advanced Programming Lecture 5 Fall 2016
Advanced Programming Lecture 5 Fall 2016
 
Icom4015 lecture4-f16
Icom4015 lecture4-f16Icom4015 lecture4-f16
Icom4015 lecture4-f16
 
Icom4015 lecture3-f16
Icom4015 lecture3-f16Icom4015 lecture3-f16
Icom4015 lecture3-f16
 
UPRM Hacks App Inventor Camp: Proposal for Sponsors
UPRM Hacks App Inventor Camp: Proposal for SponsorsUPRM Hacks App Inventor Camp: Proposal for Sponsors
UPRM Hacks App Inventor Camp: Proposal for Sponsors
 

Recently uploaded

Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Lokesh Kothari
 
Bacterial Identification and Classifications
Bacterial Identification and ClassificationsBacterial Identification and Classifications
Bacterial Identification and Classifications
Areesha Ahmad
 
GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...
GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...
GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...
Lokesh Kothari
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Sérgio Sacani
 

Recently uploaded (20)

Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
 
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
 
Kochi ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Kochi ESCORT SERVICE❤CALL GIRL
Kochi ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Kochi ESCORT SERVICE❤CALL GIRLKochi ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Kochi ESCORT SERVICE❤CALL GIRL
Kochi ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Kochi ESCORT SERVICE❤CALL GIRL
 
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICESAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICE
 
Bacterial Identification and Classifications
Bacterial Identification and ClassificationsBacterial Identification and Classifications
Bacterial Identification and Classifications
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdf
 
Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questions
 
COST ESTIMATION FOR A RESEARCH PROJECT.pptx
COST ESTIMATION FOR A RESEARCH PROJECT.pptxCOST ESTIMATION FOR A RESEARCH PROJECT.pptx
COST ESTIMATION FOR A RESEARCH PROJECT.pptx
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdf
 
GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...
GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...
GUIDELINES ON SIMILAR BIOLOGICS Regulatory Requirements for Marketing Authori...
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 

Expressions and Variables

  • 1. High-level Programming with Python Expressions and Variables Alex Ropelewski PSC-NRBSC Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer Scientist: Learning with Python Essential Computing for Bioinformatics 1
  • 2. Integer Expressions 3 >>> print 1 + 3 4 >>> print 6 * 7 42 >>> print 6 * 7 + 2 44 >>> print 2 + 6 * 7 44 >>> print 6 - 2 - 3 1 >>> print 6 - ( 2 - 3) 7 >>> print 1 / 3 0 >>> / and * higher precedence than + and - integer division truncates fractional part Operators are left associative Parenthesis can override precedence
  • 3. Floating Point Expressions 4 >>> print 1.0 / 3.0 0.333333333333 >>> print 1.0 + 2 3.0 >>> print 3.3 * 4.23 13.959 >>> print 3.3e23 * 2 6.6e+023 >>> print float(1) /3 0.333333333333 >>> Mixed operations converted to float Scientific notation allowed 12 decimal digits default precision Explicit conversion
  • 4. String Expressions 5 >>> print "aaa" aaa >>> print "aaa" + "ccc" aaaccc >>> len("aaa") 3 >>> len ("aaa" + "ccc") 6 >>> print "aaa" * 4 aaaaaaaaaaaa >>> "aaa" 'aaa' >>> "c" in "atc" True >>> "g" in "atc" False >>> + concatenates string len is a function that returns the length of its argument string any expression can be an argument * replicates strings a value is an expression that yields itself in operator finds a string inside another And returns a boolean result
  • 5. Values Can Have (MEANINGFUL) Names 6 >>> numAminoAcids = 20 >>> eValue = 6.022e23 >>> prompt = "Enter a sequence ->" >>> print numAminoAcids 20 >>> print eValue 6.022e+023 >>> print prompt Enter a sequence -> >>> print "prompt" prompt >>> >>> prompt = 5 >>> print prompt 5 >>> = binds a name to a value prints the value bound to a name = can change the value associated with a name even to a different type use Camel case for compound names
  • 6. In Bioinformatics Words … 8 >>> codon=“atg” >>> codon * 3 ’atgatgatg’ >>> seq1 =“agcgccttgaattcggcaccaggcaaatctcaaggagaagttccggggagaaggtgaaga” >>> seq2 = “cggggagtggggagttgagtcgcaagatgagcgagcggatgtccactatgagcgataata” >>> seq = seq1 + seq2 >>> seq 'agcgccttgaattcggcaccaggcaaatctcaaggagaagttccggggagaaggtgaagacggggagtggggagttgagtc gcaagatgagcgagcggatgtccactatgagcgataata‘ >>> seq[1] 'g' >>> seq[0] 'a' >>> “a” in seq True >>> len(seq1) 60 >>> len(seq) 120 First nucleotide starts at 0
  • 7. More Bioinformatics Extracting Information from Sequences 9 >>> seq[0] + seq[1] + seq[2] ’agc’ >>> seq[0:3] ’agc’ >>> seq[3:6] ’gcc’ >>> seq.count(’a’) 35 >>> seq.count(’c’) 21 >>> seq.count(’g’) 44 >>> seq.count(’t’) 12 >>> long = len(seq) >>> pctA = seq.count(’a’) >>> float(pctA) / long * 100 29.166666666666668 Find the first codon from the sequence get ’slices’ from strings: How many of each base does this sequence contain? Count the percentage of each base on the sequence.
  • 8. Additional Note About Python Strings 10 >>> seq=“ACGT” >>> print seq ACGT >>> seq=“TATATA” >>> print seq TATATA >>> seq[0] = seq[1] Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> seq[0]=seq[1] TypeError: 'str' object does not support item assignment seq = seq[1] + seq[1:] Can replace one whole string with another whole string Can NOT simply replace a sequence character with another sequence character, but… Can replace a whole string using substrings