0% found this document useful (0 votes)
13 views61 pages

Learning Python

Uploaded by

adalicc3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views61 pages

Learning Python

Uploaded by

adalicc3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Learning Python

If Statements
Strings
print(name[start:stop])
Maths
For Loop
While Loop
Random
Turtle
Graphics

turtle.pencolor(“red”)
turtle.done( )
Tuples, Lists and Dictionaries
Tuples t1 = (1, 2, 3, 4, 5)
Once a tuple is defined you cannot change what is stored in it. This means that when you write the
program you must state what the data is that is being stored in the tuple and the data cannot be
altered while the program is running. Tuples are usually used for menu items that would not need
to be changed.

Lists lis1 = [1, 2, 3, 4, 5]


The contents of a list can be changed while the program is running and lists are one of the most
common ways to store a collection of data under one variable name in Python. The data in a list
does not all have to be of the same data type. For example, the same list can store both strings and
integers; however, this can cause problems later and is therefore not recommended.

Dictionaries dict = {1:"Chaitanya","lastname":"Singh", "age":31}


The contents of a dictionary can also be changed while the program is running. Each value is
given an index or key you can define to help identify each piece of data. This index will not
change if other rows of data are added or deleted, unlike lists where the position of the items can
change and therefore their index number will also change.
Numeric Arrays
In an array, all pieces of data in that array must have the
same data type
2D Lists and Dictionaries
Reading and Writing to a Text File
To save data outside of the program and this way the data can be stored, along
with any changes that are made.
Reading and Writing to a .csv File
CSV stands for Comma Separated Values and is a format usually associated with
importing and exporting from spreadsheets and databases. It allows greater control
over the data than a simple text file, as each row is split up into identifiable
columns. Below is an example of data you may want to store.
Subprograms
Subprograms are blocks of code which perform specific tasks and can be called
upon at any time in the program to run that code.
Advantages
❑ You can write a block of code and it can be used and reused at different times
during the program.
❑ It makes the program simpler to understand as the code is grouped together
into chunks.
Tkinter GUI
Graphical User Interface
A GUI (graphical user interface) makes the program easier to use. It allows you, as
the programmer, to create screens, text boxes and buttons to help the user navigate
through the program in a more user-friendly way. Tkinter is a library of features in
Python that allows you to do this.
Look at the code below and in particular the measurements that are used in
the window.geometry and button.place lines.
More Tkinter
 changed the icon on the title bar;
 changed the background colour of the main window;
 added a static image of the logo to the top left, which will not change;
 created a label which, at the moment, displays “Hello”;
 added a Click Me button;
 added a drop-down option entitled Select Name, which will display three
names; “Bob”, “Sue” and “Tim” to the user;
 added a second image in the lower half of the window which will change
to show the photograph of the person selected from the drop-down list
when the user clicks on the Click Me button.
Please note: it is only possible to use GIF or PGM/PPM file types for images
in Tkinter as other file types are not supported.
SQLite
SQL stands for “Structured Query Language” and is the main language that
the large database packages use. SQLite is free software that can be used as an
SQL database. You can download the latest version of the software from
www.sqlite.org.
To use SQL you need to load the “DB Browser for SQLite” which you can
download from https://sqlitebrowser.org.
Understanding a Relational Database

It has four fields (ID, Name, Dept and


Salary) and 10 records in it (one for each
employee)..
In most databases you would find
repetitive data such as this. To make the
database work more efficiently the
repeated data are often stored in a
separate table. In this case there is a
department table which would store all
the information about each department to
save having to repeat all the department
details for each employee.
Part II
Chunky Challenges

You might also like