SlideShare a Scribd company logo
1 of 15
Python
A brief introductory guide
A few things about Python
• Python is a widely used general-purpose, highlevel programming language.
• Python’s main design philosophy:
– Highly readable and simple code
– As few lines of code as possible
– Dynamic type system (explain later)

• Python is used by:
– Google
– NASA
– New York Stock Exchange
Starting up
“Bare” Python is too general for scientific application, so we’re
going to use the distributives with additional modules for
graphing, statistical analysis, matrices etc.
• (OPTION 1) Download Anaconda distributive:
https://store.continuum.io/cshop/anaconda/
• (OPTION 2) Download Python(x, y) distributive:
http://code.google.com/p/pythonxy/

Install (available on Mac, Windows and Linux platforms)
Loading the IDE
IDE is a platform that lets you write code, execute programs and
test them. Both Anaconda and Python(x, y) packages have the
Spyder IDE already installed.
Search for the Spyder in the programs list, run the application.
Alternative: write code in ANY text editor, save it with a “.py”
extension, and run via the console.
Run scripts

Write code

See output

Spyder IDE
Basics: variables
a = 4 # Integer
b = 5.6 # Float (Dynamic type)
c = "hello" # String
a = "4" # rebound to String
Output variables into the console by writing:
print variable_name
Basics: operators
a=1
b=2
print
print
print
print
print
print

a+b #3
a – b # -1
a*b #2
a / b # 0 (careful with that!)
1.0 / 2 # 0.5
b ** 2 # 4 (** is a power operator)
Basics: strings
You can access substrings of a string:
string = ‘Hello World’
print string[0] # H
print string [6:] # World
You also can concatenate two strings:
string2 = ‘ Omega’
pring string + string2 # Hello World Omega
To concatenate strings and integers, use str() function
Lists
A list is a collection of values, stored in one variable.
Some number is assigned for each item in the list.
list1 = *1, 5, 12, ‘Car’+
print list1[2] # 12 (start of numeration from 0)
print len(list1) # 4 (number of elements)
You can append, delete and replace items in the list:
list1.append(10) # *1, 5, 12, ‘Car’, 10]
list1.insert(0, 2) # [2, 1, 5, 12, ‘Car’, 10+
list1.remove(‘Car’) # [2, 1, 5, 12, 10]
Dictionaries
Similar to list, but instead of numeric values, the
identifier of each element is a string:
dic = ,‘Bob’:100, ‘Jake’: 350print dic*‘Jake’+ # 350
dic *‘Henry’+ = 1000
With dictionaries, you can easily check if the specific
value is inside:
print ‘Jake’ in dic # True
Conditional statements
If-else-elif are used to check a condition and do
different stuff depending on the outcome:
dic = ,‘Bob’:100, ‘Jake’: 350if ‘Jake’ in dic:
print ‘Jake is in there’
elif ‘Bob’ in dic:
print ‘Bob is in there’ # won’t execute (why?)
else:
pass # do nothing
Very important: you have to do the indentations!
For/in Loops
Let’s rewrite the previous example:
dic = ,‘Bob’:100, ‘Jake’: 350for item in dic:
print item + ' is in there‘ # item is an index now
print item + ‘ has $’ + str(dic[item])
Bob is in there
Bob has $100
Jake is in there
Jake has $350
While loops
The logic is almost the same:
list = range(1, 101) # generate integers from 1 to 100
count = 0;
while count < len(list):
print list[count]
count += 1 # increment by 1 each time
1
2
…
File input/output
Open a file to read from it:
fin = open(‘foo.txt’)
for line in fin:
print line # manipulate each line
fin.close() # important to do after all manipulations

Write into a file:
fout = open(‘foo.txt’, ‘w’) # set the ‘write’ mode
fout.write(‘hello world’)
fout.close()
Further info
Codeacademy website:
http://www.codecademy.com/ru/tracks/python
Python official documentation:
http://docs.python.org/2/tutorial/index.html

More Related Content

Viewers also liked

Winds Air Masses Fronts
Winds Air Masses FrontsWinds Air Masses Fronts
Winds Air Masses Fronts
william seng
 
Science Bigfoot Pwpnt
Science Bigfoot PwpntScience Bigfoot Pwpnt
Science Bigfoot Pwpnt
guestff9713
 

Viewers also liked (12)

Présentation_Turquie_déc. 2010_2°partie
Présentation_Turquie_déc. 2010_2°partiePrésentation_Turquie_déc. 2010_2°partie
Présentation_Turquie_déc. 2010_2°partie
 
Testing
TestingTesting
Testing
 
Construyendo Juntos NB1 - NB2
Construyendo Juntos NB1 - NB2Construyendo Juntos NB1 - NB2
Construyendo Juntos NB1 - NB2
 
Ficha de refuerzo cs. sociales 2°
Ficha de refuerzo cs. sociales 2°Ficha de refuerzo cs. sociales 2°
Ficha de refuerzo cs. sociales 2°
 
Asae Survey On Student Memberships
Asae Survey On Student MembershipsAsae Survey On Student Memberships
Asae Survey On Student Memberships
 
dada
dadadada
dada
 
Bigfoot
BigfootBigfoot
Bigfoot
 
Recorto y aprendo
Recorto y aprendoRecorto y aprendo
Recorto y aprendo
 
Применение рыночных мультипликаторов в оценке.
Применение рыночных мультипликаторов в оценке.Применение рыночных мультипликаторов в оценке.
Применение рыночных мультипликаторов в оценке.
 
Winds Air Masses Fronts
Winds Air Masses FrontsWinds Air Masses Fronts
Winds Air Masses Fronts
 
Science Bigfoot Pwpnt
Science Bigfoot PwpntScience Bigfoot Pwpnt
Science Bigfoot Pwpnt
 
Оценка. Доходный подход
Оценка. Доходный подходОценка. Доходный подход
Оценка. Доходный подход
 

Similar to Intro to Python

Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
Sami Said
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
The program reads data from two files, itemsList-0x.txt and .docx
The program reads data from two files, itemsList-0x.txt and .docxThe program reads data from two files, itemsList-0x.txt and .docx
The program reads data from two files, itemsList-0x.txt and .docx
oscars29
 

Similar to Intro to Python (20)

Python training for beginners
Python training for beginnersPython training for beginners
Python training for beginners
 
The Ring programming language version 1.10 book - Part 7 of 212
The Ring programming language version 1.10 book - Part 7 of 212The Ring programming language version 1.10 book - Part 7 of 212
The Ring programming language version 1.10 book - Part 7 of 212
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Python slide
Python slidePython slide
Python slide
 
Compiler
CompilerCompiler
Compiler
 
The Ring programming language version 1.5.1 book - Part 5 of 180
The Ring programming language version 1.5.1 book - Part 5 of 180The Ring programming language version 1.5.1 book - Part 5 of 180
The Ring programming language version 1.5.1 book - Part 5 of 180
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
Python for dummies
Python for dummiesPython for dummies
Python for dummies
 
Python_IoT.pptx
Python_IoT.pptxPython_IoT.pptx
Python_IoT.pptx
 
The Ring programming language version 1.5.1 book - Part 13 of 180
The Ring programming language version 1.5.1 book - Part 13 of 180The Ring programming language version 1.5.1 book - Part 13 of 180
The Ring programming language version 1.5.1 book - Part 13 of 180
 
D programming language
D programming languageD programming language
D programming language
 
Python
PythonPython
Python
 
The Ring programming language version 1.7 book - Part 17 of 196
The Ring programming language version 1.7 book - Part 17 of 196The Ring programming language version 1.7 book - Part 17 of 196
The Ring programming language version 1.7 book - Part 17 of 196
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
The program reads data from two files, itemsList-0x.txt and .docx
The program reads data from two files, itemsList-0x.txt and .docxThe program reads data from two files, itemsList-0x.txt and .docx
The program reads data from two files, itemsList-0x.txt and .docx
 
Basics of Python Programming in one PDF File.pdf
Basics of Python Programming in one PDF File.pdfBasics of Python Programming in one PDF File.pdf
Basics of Python Programming in one PDF File.pdf
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Intro to Python

  • 2. A few things about Python • Python is a widely used general-purpose, highlevel programming language. • Python’s main design philosophy: – Highly readable and simple code – As few lines of code as possible – Dynamic type system (explain later) • Python is used by: – Google – NASA – New York Stock Exchange
  • 3. Starting up “Bare” Python is too general for scientific application, so we’re going to use the distributives with additional modules for graphing, statistical analysis, matrices etc. • (OPTION 1) Download Anaconda distributive: https://store.continuum.io/cshop/anaconda/ • (OPTION 2) Download Python(x, y) distributive: http://code.google.com/p/pythonxy/ Install (available on Mac, Windows and Linux platforms)
  • 4. Loading the IDE IDE is a platform that lets you write code, execute programs and test them. Both Anaconda and Python(x, y) packages have the Spyder IDE already installed. Search for the Spyder in the programs list, run the application. Alternative: write code in ANY text editor, save it with a “.py” extension, and run via the console.
  • 5. Run scripts Write code See output Spyder IDE
  • 6. Basics: variables a = 4 # Integer b = 5.6 # Float (Dynamic type) c = "hello" # String a = "4" # rebound to String Output variables into the console by writing: print variable_name
  • 7. Basics: operators a=1 b=2 print print print print print print a+b #3 a – b # -1 a*b #2 a / b # 0 (careful with that!) 1.0 / 2 # 0.5 b ** 2 # 4 (** is a power operator)
  • 8. Basics: strings You can access substrings of a string: string = ‘Hello World’ print string[0] # H print string [6:] # World You also can concatenate two strings: string2 = ‘ Omega’ pring string + string2 # Hello World Omega To concatenate strings and integers, use str() function
  • 9. Lists A list is a collection of values, stored in one variable. Some number is assigned for each item in the list. list1 = *1, 5, 12, ‘Car’+ print list1[2] # 12 (start of numeration from 0) print len(list1) # 4 (number of elements) You can append, delete and replace items in the list: list1.append(10) # *1, 5, 12, ‘Car’, 10] list1.insert(0, 2) # [2, 1, 5, 12, ‘Car’, 10+ list1.remove(‘Car’) # [2, 1, 5, 12, 10]
  • 10. Dictionaries Similar to list, but instead of numeric values, the identifier of each element is a string: dic = ,‘Bob’:100, ‘Jake’: 350print dic*‘Jake’+ # 350 dic *‘Henry’+ = 1000 With dictionaries, you can easily check if the specific value is inside: print ‘Jake’ in dic # True
  • 11. Conditional statements If-else-elif are used to check a condition and do different stuff depending on the outcome: dic = ,‘Bob’:100, ‘Jake’: 350if ‘Jake’ in dic: print ‘Jake is in there’ elif ‘Bob’ in dic: print ‘Bob is in there’ # won’t execute (why?) else: pass # do nothing Very important: you have to do the indentations!
  • 12. For/in Loops Let’s rewrite the previous example: dic = ,‘Bob’:100, ‘Jake’: 350for item in dic: print item + ' is in there‘ # item is an index now print item + ‘ has $’ + str(dic[item]) Bob is in there Bob has $100 Jake is in there Jake has $350
  • 13. While loops The logic is almost the same: list = range(1, 101) # generate integers from 1 to 100 count = 0; while count < len(list): print list[count] count += 1 # increment by 1 each time 1 2 …
  • 14. File input/output Open a file to read from it: fin = open(‘foo.txt’) for line in fin: print line # manipulate each line fin.close() # important to do after all manipulations Write into a file: fout = open(‘foo.txt’, ‘w’) # set the ‘write’ mode fout.write(‘hello world’) fout.close()
  • 15. Further info Codeacademy website: http://www.codecademy.com/ru/tracks/python Python official documentation: http://docs.python.org/2/tutorial/index.html