SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
Introduction to Python -1
Ahmad Hussein
ahmadhussein.ah7@gmail.com
Syntax
Hello World
print("hello world”)
Indentation
• Most languages don’t care about indentation.
• Most humans do.
• We tend to group similar things together.
Indentation
• The else here actually belongs to the 2nd if statement.
//java
if(true)
if(false)
System.out.println("Hello1 !");
else
System.out.println("Hello2 !");
Indentation
• The else here actually belongs to the 2nd if statement.
//java
if(true){
if(false){
System.out.println("Hello1 !");
}else{
System.out.println("Hello2 !");
}}
Indentation
• I knew a coder like this :D
//java
if(true)
if(false)
System.out.println("Hello1 !");
else
System.out.println("Hello2 !");
Indentation
• You should always be explicit.
//java
if(true){
if(false){
System.out.println("Hello1 !");
}else{
System.out.println("Hello2 !");
}
}
Indentation
• Python embraces indentation.
# python
if True:
if False:
print("Hello1 !")
else:
print("Hello2 !")
Comments
• ( # ) A traditional one line comment
• “””
any string not assigned to a variable is considered comment.
this is an example of a multi-line comment.
“””
• “ this is a single line comment ”
Types
strings
# This is a string
Name = "Ahmad Hussein (that's me) "
# This is also a string
Home = 'Damascus, Syria’
# This is a multi-line string
Sites = ''' You can find me online on
Sites like LinkedIn and Facebook.'''
# This is also a multi-line string
Bio = """ If you don’t find me online
You can find me outside. """
Numbers
# Integer Number
year = 2010
year = int("2010")
# Floating Point Numbers
pi = 3.14159265
pi = float("3.14159265")
# Fixed Point Number
from decimal import Decimal
price = Decimal("0.02")
Null
Optional_data = None
Lists
# Lists can be heterogenous
favorites = []
# Appending
favorites.append(42)
# Extending
favorites.extend(['Python' , True])
# Equivalent to
favorites = [42 , 'Python' , True]
Lists
numbers = [ 1 , 2 , 3 , 4 , 5 ]
len(numbers)
# 5
numbers[0]
# 1
numbers[0:2]
# [1 ,2]
numbers[2:]
# [3, 4, 5]
Dictionaries
person = {}
# Set by key / Get by key
person['name'] = 'Ahmad Hussein'
# Update
person.update({
'favorites' : [42 , 'food'] ,
'gender' : 'male'
})
# Any immutable object can be a dictionary key
person[42] = 'favorite number'
person[(44.47 , -73.21)] = 'coordinates'
Dictionary Methods
person = { 'name' : 'karim' , 'gender' : 'Male'}
person['name']
person.get('name')
# 'karim'
person.keys()
# ['name' , 'gender']
person.values()
# ['karim' , 'Male']
person.items()
# [['name' , 'karim'] , ['gender' , 'Male']]
Booleans
Is_python = True
# Everything in python can be cast to Boolean
Is_pyhton = bool('any object’)
# All of these things are equivalent to False
These_are_false = False or 0 or "" or {} or [] or None
# Most everything else is equivalent to True
These_are_true = True and 1 and 'Text' and {'a' : 'b'} and ['c','d']
Operators
Arithmetic
a = 10 # 10
a += 1 # 11
a -= 1 # 10
b = a + 1 # 11
c = a - 1 # 9
d = a * 2 # 20
e = a / 2 # 5
f = a % 3 # 1
g = a ** 2 # 100
String Manipulation
animals = 'Cats ' + 'Dogs '
animals += 'Rabbits'
# Cats Dogs Rabbits
fruit = ', '.join(['Apple' , 'Banana' , 'Orange'])
#Apple, Banana, Orange
date = '%s %d %d' % ('Mar' , 20 , 2018)
# Mar 20 2018
name = '%(first)s %(last)s' % {
'first' : 'Ahmad' ,
'last' : 'Hussein'}
# Ahmad Hussein
Logical Comparison
# Logical And
a and b
# Logical Or
a or b
# Logical Negation
not a
# Compound
(a and not (b or c))
Identity Comparison
1 is 1 == True
# Non Identity
1 is not '1' == True
# Example
bool(1) == True
bool(True) == True
1 and True == True
1 is True == False
Arithmetic Comparison
# Ordering
a > b
a >= b
a < b
a <= b
# Equality / Difference
a == b
a != b
Control Flow
Conditionals
grade = 82
if grade >= 90:
if grade == 100:
print('A+')
else:
print ('A')
elif grade >= 80:
print ('B')
elif grade >= 70:
print ('C')
else:
print('E’)
# B
For Loop
for x in range(10):
print(x)
# 0-9
fruits = ['Apple' , 'Orange']
for fruit in fruits:
print(fruit)
Expanded For Loop
countries = {
'SY' : 'Syria' ,
'US' : 'United States'
}
for key, value in countries.items():
print('%s : %s' % (key, value))
While Loop
x = 0
while x < 100 :
print(x)
x +=1
List comprehensions
• Useful for replacing simple for-loops.
odds = []
for x in range(50):
if x % 2 :
odds.append(x)
odds = [x for x in range(50) if x % 2]
functions
• Assignment : Fibonacci method !
def my_function():
"""Function Documentation """
print('Hello Word’)
def add( x , y ):
return x + y

Contenu connexe

Tendances

Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
Wen-Tien Chang
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 

Tendances (20)

Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
Hashes
HashesHashes
Hashes
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Clean code
Clean codeClean code
Clean code
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
Python- strings
Python- stringsPython- strings
Python- strings
 
Php
PhpPhp
Php
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Php array
Php arrayPhp array
Php array
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 

Similaire à Python introduction 1

Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
Hassen Poreya
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
ICADCMLTPC
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
anzhong70
 

Similaire à Python introduction 1 (20)

Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
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
 
Python an-intro - odp
Python an-intro - odpPython an-intro - odp
Python an-intro - odp
 
Elixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental ConceptsElixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental Concepts
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Python in One Shot.docx
Python in One Shot.docxPython in One Shot.docx
Python in One Shot.docx
 
Python in one shot
Python in one shotPython in one shot
Python in one shot
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
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
 
SQL -PHP Tutorial
SQL -PHP TutorialSQL -PHP Tutorial
SQL -PHP Tutorial
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
 
Τα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonΤα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την Python
 

Plus de Ahmad Hussein (6)

Knowledge Representation Methods
Knowledge Representation MethodsKnowledge Representation Methods
Knowledge Representation Methods
 
Knowledge-Base Systems Homework - 2019
Knowledge-Base Systems Homework - 2019Knowledge-Base Systems Homework - 2019
Knowledge-Base Systems Homework - 2019
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Knowledge based systems homework
Knowledge based systems homeworkKnowledge based systems homework
Knowledge based systems homework
 
Expert system with python -2
Expert system with python  -2Expert system with python  -2
Expert system with python -2
 
Expert System With Python -1
Expert System With Python -1Expert System With Python -1
Expert System With Python -1
 

Dernier

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
amitlee9823
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
amitlee9823
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
amitlee9823
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
AroojKhan71
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
amitlee9823
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
JoseMangaJr1
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 

Dernier (20)

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 

Python introduction 1

  • 1. Introduction to Python -1 Ahmad Hussein ahmadhussein.ah7@gmail.com
  • 4. Indentation • Most languages don’t care about indentation. • Most humans do. • We tend to group similar things together.
  • 5. Indentation • The else here actually belongs to the 2nd if statement. //java if(true) if(false) System.out.println("Hello1 !"); else System.out.println("Hello2 !");
  • 6. Indentation • The else here actually belongs to the 2nd if statement. //java if(true){ if(false){ System.out.println("Hello1 !"); }else{ System.out.println("Hello2 !"); }}
  • 7. Indentation • I knew a coder like this :D //java if(true) if(false) System.out.println("Hello1 !"); else System.out.println("Hello2 !");
  • 8. Indentation • You should always be explicit. //java if(true){ if(false){ System.out.println("Hello1 !"); }else{ System.out.println("Hello2 !"); } }
  • 9. Indentation • Python embraces indentation. # python if True: if False: print("Hello1 !") else: print("Hello2 !")
  • 10. Comments • ( # ) A traditional one line comment • “”” any string not assigned to a variable is considered comment. this is an example of a multi-line comment. “”” • “ this is a single line comment ”
  • 11. Types
  • 12. strings # This is a string Name = "Ahmad Hussein (that's me) " # This is also a string Home = 'Damascus, Syria’ # This is a multi-line string Sites = ''' You can find me online on Sites like LinkedIn and Facebook.''' # This is also a multi-line string Bio = """ If you don’t find me online You can find me outside. """
  • 13. Numbers # Integer Number year = 2010 year = int("2010") # Floating Point Numbers pi = 3.14159265 pi = float("3.14159265") # Fixed Point Number from decimal import Decimal price = Decimal("0.02")
  • 15. Lists # Lists can be heterogenous favorites = [] # Appending favorites.append(42) # Extending favorites.extend(['Python' , True]) # Equivalent to favorites = [42 , 'Python' , True]
  • 16. Lists numbers = [ 1 , 2 , 3 , 4 , 5 ] len(numbers) # 5 numbers[0] # 1 numbers[0:2] # [1 ,2] numbers[2:] # [3, 4, 5]
  • 17. Dictionaries person = {} # Set by key / Get by key person['name'] = 'Ahmad Hussein' # Update person.update({ 'favorites' : [42 , 'food'] , 'gender' : 'male' }) # Any immutable object can be a dictionary key person[42] = 'favorite number' person[(44.47 , -73.21)] = 'coordinates'
  • 18. Dictionary Methods person = { 'name' : 'karim' , 'gender' : 'Male'} person['name'] person.get('name') # 'karim' person.keys() # ['name' , 'gender'] person.values() # ['karim' , 'Male'] person.items() # [['name' , 'karim'] , ['gender' , 'Male']]
  • 19. Booleans Is_python = True # Everything in python can be cast to Boolean Is_pyhton = bool('any object’) # All of these things are equivalent to False These_are_false = False or 0 or "" or {} or [] or None # Most everything else is equivalent to True These_are_true = True and 1 and 'Text' and {'a' : 'b'} and ['c','d']
  • 21. Arithmetic a = 10 # 10 a += 1 # 11 a -= 1 # 10 b = a + 1 # 11 c = a - 1 # 9 d = a * 2 # 20 e = a / 2 # 5 f = a % 3 # 1 g = a ** 2 # 100
  • 22. String Manipulation animals = 'Cats ' + 'Dogs ' animals += 'Rabbits' # Cats Dogs Rabbits fruit = ', '.join(['Apple' , 'Banana' , 'Orange']) #Apple, Banana, Orange date = '%s %d %d' % ('Mar' , 20 , 2018) # Mar 20 2018 name = '%(first)s %(last)s' % { 'first' : 'Ahmad' , 'last' : 'Hussein'} # Ahmad Hussein
  • 23. Logical Comparison # Logical And a and b # Logical Or a or b # Logical Negation not a # Compound (a and not (b or c))
  • 24. Identity Comparison 1 is 1 == True # Non Identity 1 is not '1' == True # Example bool(1) == True bool(True) == True 1 and True == True 1 is True == False
  • 25. Arithmetic Comparison # Ordering a > b a >= b a < b a <= b # Equality / Difference a == b a != b
  • 27. Conditionals grade = 82 if grade >= 90: if grade == 100: print('A+') else: print ('A') elif grade >= 80: print ('B') elif grade >= 70: print ('C') else: print('E’) # B
  • 28. For Loop for x in range(10): print(x) # 0-9 fruits = ['Apple' , 'Orange'] for fruit in fruits: print(fruit)
  • 29. Expanded For Loop countries = { 'SY' : 'Syria' , 'US' : 'United States' } for key, value in countries.items(): print('%s : %s' % (key, value))
  • 30. While Loop x = 0 while x < 100 : print(x) x +=1
  • 31. List comprehensions • Useful for replacing simple for-loops. odds = [] for x in range(50): if x % 2 : odds.append(x) odds = [x for x in range(50) if x % 2]
  • 32. functions • Assignment : Fibonacci method ! def my_function(): """Function Documentation """ print('Hello Word’) def add( x , y ): return x + y