SlideShare a Scribd company logo
‫‪Play with Python‬‬
                          ‫1‪Lab‬‬
‫كل الشرح في المحاضرة، أي مفاهيم في المعامل :قاعدة هامة‬
          ‫ستكون مغطاه في المحاضرة قبل المعمل‬
          ‫المطلوب فقط شرح المطلوب من التمرين‬
                      ‫ودعهم ينطلقون‬
Agenda
•   Setup the Environment and Hello World
•   Exercise 1:
    o   Factorial then Factorial with list
•   Exercise 2:
    o   character count and histogram
•   Fun:
    o   Plot!
Setup the Environment
•   Install Python 2.7.3:
    o   python-2.7.3.msi
•   Install Aptana IDE:
    o   Aptana_Studio_3_Setup_3.2.0.exe
Setup the Environment
  1- Open your Python IDLE from:
  Start >> All Programs >> Python 2.7 >>
IDLE

  2- Write your first program:
  print "Hello World"
  Press Enter
Setup the Environment (Your first project)
3- Start Aptana:
         Start >> All Programs >> Aptana Studio 3
         press ok for the workspace
4- Create your first project:
         Select File >> New >> Project ... >> PyDev Project



5-Enter Project Name: lab1
6-(for first time only) Click on "Please configure an interpreter ... "
7- click auto Config
8- click ok
9- then ok (wait till Aptana finishes ...)
10-create your main file by:
              right click on "lab1" in Package Explorer >> New >> PyDev Module
11- Enter Name: main
12- click ok
Screenshots (steps 3- 12)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Setup the Environment (Your first project)
Write the following code in your main file:
      n = 5
      for i in range(1, n):
       print "Hello World"


click on run as from the toolbar


Select "Python Run "


Click Ok (See the run in Console)
Setup the Environment (Your first project)
put your previous code in a function like this
      def main():
       n = 5
       for i in range(1, n):
          print "Hello World"


   then call it:
   main()
Setup the Environment (Your first project)
again change your main to return 2 values, "hello world"
  string and its length
      def main():
       s = "Hello World"
       return "Hello World", len(s)


   then print the return of main like this:
   print main()
      ('Hello World', 11)
Exercise 1
Write this program in your main file:
def count_char(str):
       d = {}
       for char in str:
              if char in d:
                      d[char] += 1
              else:
                      d[char] = 1
       return d
print count_char("My name is FCIS tell me every thing you know
   !")



{' ': 9, 'C': 1, 'F': 1, 'I': 2, 'M': 1, 'S': 1, 'a': 1, 'e': 5, 'g': 1, 'i': 2, 'h': 1, 'k': 1, 'm': 2, 'l': 2, 'o': 1,
      'n': 3, 's': 1, 'r': 1, 't': 2, 'w': 1, 'v': 1, 'y': 2}
This program counts the occurrences of characters in given string and returns a
    dictionary containing for every key character an integer value
Exercise 1 (10 minutes)
Add a new function draw_historgram() that draws an ascii histogram of a given
   dictionary (from count_char()) like this:
a   :   * *
    :   * * * * * * * * *
e   :   * * * * *
d   :   *
g   :   *
i   :   * *
M   :   *
k   :   *
v   :   *
m   :   * * * *
l   :   * *
o   :   * *
...
Exercise 1 (Solution)
def draw_historgram(d):
    for char in d:
        print char, " : ",
        for c in range(1, d[char]+1):
            print "*",
        print ""
Exercise 2
Write this program in your main file:
def fact(n):
     if (n <= 1):
            return 1
     else:
            return n * fact(n - 1)


print fact(5)
120


this program computes a factorial for a given integer
Exercise 2 (15 minutes)
Modify this function to make it return the factorial, and also
  a list contains all factorials so far like this:


print fact(5)
(120, [1, 2, 6, 24, 120])


the list contains all the factorials from 1 to 5
Exercise 2 (Solution)
def fact(n):
      if (n <= 1):
             return 1, [1]
      else:
             prev_fact, theList = fact(n - 1)


             current_fact = n* prev_fact
             theList.append(current_fact)


             return current_fact, theList



print fact(5)



Note: in this exercise the student should be able to implement the solution with the multiple return values ( tuple), this
    python feature is explained in the lecture
This exercise is to let the students think the in new way of programming, to feel change from C/C++/C3/Java traditional
     languages
Have Fun
Install Pythonxy:
(Click yes for every question the installer asks about
   existing python setup)


Open Spyder by:(Start>>python(x,y) >> Spyder >> Spyder
  )


Copy and paste your Fact() function to the python
  interpreter, and plot Fact() like this:
f, l = fact(5)
plot(l)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Python summer course  play with python  (lab1)
Thank You

More Related Content

ODP
Stackless Python 101
PDF
The Ring programming language version 1.10 book - Part 35 of 212
PPTX
Smarter Testing with Spock
PDF
Use C++ to Manipulate mozSettings in Gecko
PPTX
Testing in Python: doctest and unittest
PPTX
Testing in Python: doctest and unittest (Updated)
PDF
DCN Practical
PDF
Effective Modern C++ - Item 35 & 36
Stackless Python 101
The Ring programming language version 1.10 book - Part 35 of 212
Smarter Testing with Spock
Use C++ to Manipulate mozSettings in Gecko
Testing in Python: doctest and unittest
Testing in Python: doctest and unittest (Updated)
DCN Practical
Effective Modern C++ - Item 35 & 36

What's hot (19)

PPTX
Python Programming Essentials - M25 - os and sys modules
PDF
The Ring programming language version 1.7 book - Part 30 of 196
PDF
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
PDF
PDF
The Ring programming language version 1.5.3 book - Part 25 of 184
PDF
Java_practical_handbook
PDF
Spock Testing Framework - The Next Generation
PDF
The Ring programming language version 1.5.1 book - Part 32 of 180
TXT
PPTX
4. functions
DOCX
Java practical
PPTX
Java simple programs
PDF
Spock Framework - Slidecast
PDF
The Ring programming language version 1.2 book - Part 16 of 84
PDF
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
PDF
Python Unit Test
PDF
Natural language processing open seminar For Tensorflow usage
PPTX
Java 7, 8 & 9 - Moving the language forward
PDF
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Python Programming Essentials - M25 - os and sys modules
The Ring programming language version 1.7 book - Part 30 of 196
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
The Ring programming language version 1.5.3 book - Part 25 of 184
Java_practical_handbook
Spock Testing Framework - The Next Generation
The Ring programming language version 1.5.1 book - Part 32 of 180
4. functions
Java practical
Java simple programs
Spock Framework - Slidecast
The Ring programming language version 1.2 book - Part 16 of 84
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
Python Unit Test
Natural language processing open seminar For Tensorflow usage
Java 7, 8 & 9 - Moving the language forward
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Ad

Similar to Python summer course play with python (lab1) (20)

PDF
analysis of data structure design programs
PDF
Python program For O level Practical
PDF
III MCS python lab (1).pdf
PPTX
Python programming workshop session 4
PPTX
Working with functions.pptx. Hb.
PDF
xii cs practicals class 12 computer science.pdf
PDF
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
PDF
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
PDF
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PDF
What's new in Python 3.11
PPTX
Chapter 02 functions -class xii
PDF
Revision of the basics of python - KVSRO Patna.pdf
PDF
Chapter 1 Class 12 Computer Science Unit 1
DOC
Labsheet_3
PDF
Revision of the basics of python1 (1).pdf
PDF
Python tour
DOCX
Question 1 briefly respond to all the following questions. make
PDF
python lab programs.pdf
PDF
The Ring programming language version 1.3 book - Part 18 of 88
PPTX
Python functions PYTHON FUNCTIONS1234567
analysis of data structure design programs
Python program For O level Practical
III MCS python lab (1).pdf
Python programming workshop session 4
Working with functions.pptx. Hb.
xii cs practicals class 12 computer science.pdf
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
What's new in Python 3.11
Chapter 02 functions -class xii
Revision of the basics of python - KVSRO Patna.pdf
Chapter 1 Class 12 Computer Science Unit 1
Labsheet_3
Revision of the basics of python1 (1).pdf
Python tour
Question 1 briefly respond to all the following questions. make
python lab programs.pdf
The Ring programming language version 1.3 book - Part 18 of 88
Python functions PYTHON FUNCTIONS1234567
Ad

Recently uploaded (20)

PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Spectroscopy.pptx food analysis technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Tartificialntelligence_presentation.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Getting Started with Data Integration: FME Form 101
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25-Week II
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
Spectroscopy.pptx food analysis technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Tartificialntelligence_presentation.pptx
A comparative analysis of optical character recognition models for extracting...
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Getting Started with Data Integration: FME Form 101
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
20250228 LYD VKU AI Blended-Learning.pptx
Group 1 Presentation -Planning and Decision Making .pptx

Python summer course play with python (lab1)

  • 1. ‫‪Play with Python‬‬ ‫1‪Lab‬‬ ‫كل الشرح في المحاضرة، أي مفاهيم في المعامل :قاعدة هامة‬ ‫ستكون مغطاه في المحاضرة قبل المعمل‬ ‫المطلوب فقط شرح المطلوب من التمرين‬ ‫ودعهم ينطلقون‬
  • 2. Agenda • Setup the Environment and Hello World • Exercise 1: o Factorial then Factorial with list • Exercise 2: o character count and histogram • Fun: o Plot!
  • 3. Setup the Environment • Install Python 2.7.3: o python-2.7.3.msi • Install Aptana IDE: o Aptana_Studio_3_Setup_3.2.0.exe
  • 4. Setup the Environment 1- Open your Python IDLE from: Start >> All Programs >> Python 2.7 >> IDLE 2- Write your first program: print "Hello World" Press Enter
  • 5. Setup the Environment (Your first project) 3- Start Aptana: Start >> All Programs >> Aptana Studio 3 press ok for the workspace 4- Create your first project: Select File >> New >> Project ... >> PyDev Project 5-Enter Project Name: lab1 6-(for first time only) Click on "Please configure an interpreter ... " 7- click auto Config 8- click ok 9- then ok (wait till Aptana finishes ...) 10-create your main file by: right click on "lab1" in Package Explorer >> New >> PyDev Module 11- Enter Name: main 12- click ok
  • 22. Setup the Environment (Your first project) Write the following code in your main file: n = 5 for i in range(1, n): print "Hello World" click on run as from the toolbar Select "Python Run " Click Ok (See the run in Console)
  • 23. Setup the Environment (Your first project) put your previous code in a function like this def main(): n = 5 for i in range(1, n): print "Hello World" then call it: main()
  • 24. Setup the Environment (Your first project) again change your main to return 2 values, "hello world" string and its length def main(): s = "Hello World" return "Hello World", len(s) then print the return of main like this: print main() ('Hello World', 11)
  • 25. Exercise 1 Write this program in your main file: def count_char(str): d = {} for char in str: if char in d: d[char] += 1 else: d[char] = 1 return d print count_char("My name is FCIS tell me every thing you know !") {' ': 9, 'C': 1, 'F': 1, 'I': 2, 'M': 1, 'S': 1, 'a': 1, 'e': 5, 'g': 1, 'i': 2, 'h': 1, 'k': 1, 'm': 2, 'l': 2, 'o': 1, 'n': 3, 's': 1, 'r': 1, 't': 2, 'w': 1, 'v': 1, 'y': 2} This program counts the occurrences of characters in given string and returns a dictionary containing for every key character an integer value
  • 26. Exercise 1 (10 minutes) Add a new function draw_historgram() that draws an ascii histogram of a given dictionary (from count_char()) like this: a : * * : * * * * * * * * * e : * * * * * d : * g : * i : * * M : * k : * v : * m : * * * * l : * * o : * * ...
  • 27. Exercise 1 (Solution) def draw_historgram(d): for char in d: print char, " : ", for c in range(1, d[char]+1): print "*", print ""
  • 28. Exercise 2 Write this program in your main file: def fact(n): if (n <= 1): return 1 else: return n * fact(n - 1) print fact(5) 120 this program computes a factorial for a given integer
  • 29. Exercise 2 (15 minutes) Modify this function to make it return the factorial, and also a list contains all factorials so far like this: print fact(5) (120, [1, 2, 6, 24, 120]) the list contains all the factorials from 1 to 5
  • 30. Exercise 2 (Solution) def fact(n): if (n <= 1): return 1, [1] else: prev_fact, theList = fact(n - 1) current_fact = n* prev_fact theList.append(current_fact) return current_fact, theList print fact(5) Note: in this exercise the student should be able to implement the solution with the multiple return values ( tuple), this python feature is explained in the lecture This exercise is to let the students think the in new way of programming, to feel change from C/C++/C3/Java traditional languages
  • 31. Have Fun Install Pythonxy: (Click yes for every question the installer asks about existing python setup) Open Spyder by:(Start>>python(x,y) >> Spyder >> Spyder ) Copy and paste your Fact() function to the python interpreter, and plot Fact() like this: f, l = fact(5) plot(l)