This document provides an introduction to Python programming. It discusses that Python is an interpretive language and describes advantages like automatic memory management and disadvantages like slower speed compared to compiled languages. It also covers Python versions, the Anaconda IDE, variables and data types, operators, strings, lists, tuples, dictionaries, conditional statements, loops and functions like range(). Examples are provided throughout to demonstrate the core concepts of Python syntax and programming.
This document provides an introduction to Python programming. It discusses that Python is an interpreted language and contrasts this with compiled languages. The document then covers Python basics like variables, data types, operators, conditional statements, loops and lists. It introduces key concepts like for loops, while loops, the range() function, break and continue statements. The goal is to provide a practical overview of common Python aspects for new programmers.
This document provides an introduction to Python programming. It discusses that Python is an interpreted language and covers advantages like automatic memory management and disadvantages like slower speed compared to compiled languages. It also discusses Python versions and the popular Anaconda IDE. The document then covers key Python concepts like variables, data types, operators, strings, lists, dictionaries, conditionals and loops. It provides examples to demonstrate how to use these core Python elements.
This document provides an overview and introduction to Python programming. It discusses Python basics like variables, data types, operators, conditionals, loops, functions and file handling. It also covers commonly used Python libraries and concepts in data analytics like NumPy, Pandas, Matplotlib and statistics. The document is intended as a whistle-stop tour to cover the most common aspects of Python.
This document provides a high-level summary of an introduction to Python programming course. The summary includes an overview of Python basics like variables, data types, operators, conditionals, loops, functions and file handling. It also discusses commonly used Python libraries and concepts in data analytics like NumPy, Pandas, Matplotlib and statistics.
web programming UNIT VIII python by Bhavsingh MalothBhavsingh Maloth
油
This document provides a tutorial on Python programming. It introduces core Python concepts over several sections. The first section discusses what will be covered, including an introduction to the Python language and becoming comfortable writing basic programs. Subsequent sections cover specific Python topics like data types, operators, conditional and loop execution, functions, modules and packages for code reusability. The document emphasizes consistent indentation and readability in Python code.
This is a presentation which is an introduction to python language.
The presentation is contributed by me for educational purpose and this presentation is
Only introduction.
The Basic python data types and how to use python for Data Science,
This document provides an introduction and overview of the Python programming language. It discusses what Python is, why to learn a scripting language and why Python specifically. It covers how to install Python and how to edit Python code using IDLE. The rest of the document demonstrates various Python concepts like variables, data types, operators, flow control statements, functions and more through sample code examples. Each code example is accompanied by instructions to run the sample script in IDLE.
This tutorial provides an introduction to the Python programming language. It will cover Python's core features like syntax, data types, operators, conditional and loop execution, functions, modules and packages to enable writing basic programs. The tutorial is intended for learners to learn Python together through questions, discussions and pointing out mistakes.
The document provides an introduction to Python programming, including details about Python's history, versions, data types, strings, and code execution. It discusses how to install Python and write basic programs. Key reasons for using Python are its object-oriented nature, readability, large standard library, cross-platform capabilities, and ease of use. The document also covers string methods and slicing, numeric data types, installing Python, and running code in interactive and script modes.
python programming language Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. INTRODUCTION
HISTORY
USES OF PYTHON
FEATURES OF PYTHON
PYTHON PROJECT FOR BEGINNERS
PYTHON PROGRAM
KEY CHANGES IN PYTHON
BASIC SYNTAX
VARIABLE
NUMBERS
STANDARD TYPE HIERARCHY
STRING
CONDITIONALS
FOR LOOP
FUNCTION
KEYWORDS
WHY PYTHON ?
DIFFERENTIATE
EXAMPLES
Python is a cross-platform programming language that can run on Windows, macOS, Linux, and other platforms. It is free and open-source. While most modern operating systems come with Python pre-installed, it is generally recommended to install the latest version separately. The Thonny IDE is a simple Python IDE that bundles the latest Python version, so it can run Python programs without requiring a separate Python installation. Thonny has features like an easy-to-use debugger and code completion that make it suitable for beginners learning Python.
Python is a high-level, interpreted, interactive and object-oriented scripting language. It is designed to be highly readable using English keywords. Python is interpreted at runtime and does not require compilation. It supports both procedural and object-oriented programming. Python is beginner friendly and supports a wide range of applications. It is portable, extensible, and has a large standard library. Variables are dynamically typed and support integers, floating point numbers, complex numbers, strings, lists, tuples and dictionaries.
Python can be used for a variety of applications including web development, scientific computing, education, desktop GUIs, and software development. It is commonly used to build web applications using frameworks like Django and Flask, for scientific computing tasks using libraries like NumPy and SciPy, and for general software development tasks like build automation and testing. Python supports a range of data types including integers, floats, complex numbers, lists, dictionaries, sets, and strings. It can be used to write functions and programs to solve problems across many domains.
Basic concept of Python.pptx includes design tool, identifier, variables.supriyasarkar38
油
This document discusses Python programming concepts including data types, variables, operators, and functions. It provides examples of Python syntax for writing and executing code as well as built-in data types like strings, integers, and lists. Key concepts covered include variables, data type casting, comments, arithmetic and comparison operators, and functions.
This document provides information about Jupyter Notebook, including:
- Jupyter Notebook is an open-source web application for creating and sharing documents containing live code, equations, visualizations, and narrative text.
- It works locally on localhost port 8888 and the easiest way to install it is through Anaconda which includes Jupyter Notebook and popular Python libraries.
- Notebooks use kernels to run code in different programming languages, with IPython being the default Python kernel.
¥ 艶COPY & PASTE LINK https://crack4pro.net/download-latest-windows-softwaresz/
Wondershare Filmora 14.3.2 Crack is an excellent software for video editing. It is beneficial in perfect video editing with industry standards.
AnyDesk Pro 3.7.0 Crack License Key Free Download 2025 [Latest]haroonsaeed605
油
copy and past on google 油ぬも https://mediiafiire.com/
"AnyDesk Pro: Secure, fast, and reliable remote desktop software for professionals. Access and control devices from anywhere with low latency, high performance, and top-tier security. Perfect for businesses and IT teams. Try Any Desk Pro today!"
More Related Content
Similar to Q-Step_WS_02102019_Practical_introduction_to_Python.pdf (20)
This is a presentation which is an introduction to python language.
The presentation is contributed by me for educational purpose and this presentation is
Only introduction.
The Basic python data types and how to use python for Data Science,
This document provides an introduction and overview of the Python programming language. It discusses what Python is, why to learn a scripting language and why Python specifically. It covers how to install Python and how to edit Python code using IDLE. The rest of the document demonstrates various Python concepts like variables, data types, operators, flow control statements, functions and more through sample code examples. Each code example is accompanied by instructions to run the sample script in IDLE.
This tutorial provides an introduction to the Python programming language. It will cover Python's core features like syntax, data types, operators, conditional and loop execution, functions, modules and packages to enable writing basic programs. The tutorial is intended for learners to learn Python together through questions, discussions and pointing out mistakes.
The document provides an introduction to Python programming, including details about Python's history, versions, data types, strings, and code execution. It discusses how to install Python and write basic programs. Key reasons for using Python are its object-oriented nature, readability, large standard library, cross-platform capabilities, and ease of use. The document also covers string methods and slicing, numeric data types, installing Python, and running code in interactive and script modes.
python programming language Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. INTRODUCTION
HISTORY
USES OF PYTHON
FEATURES OF PYTHON
PYTHON PROJECT FOR BEGINNERS
PYTHON PROGRAM
KEY CHANGES IN PYTHON
BASIC SYNTAX
VARIABLE
NUMBERS
STANDARD TYPE HIERARCHY
STRING
CONDITIONALS
FOR LOOP
FUNCTION
KEYWORDS
WHY PYTHON ?
DIFFERENTIATE
EXAMPLES
Python is a cross-platform programming language that can run on Windows, macOS, Linux, and other platforms. It is free and open-source. While most modern operating systems come with Python pre-installed, it is generally recommended to install the latest version separately. The Thonny IDE is a simple Python IDE that bundles the latest Python version, so it can run Python programs without requiring a separate Python installation. Thonny has features like an easy-to-use debugger and code completion that make it suitable for beginners learning Python.
Python is a high-level, interpreted, interactive and object-oriented scripting language. It is designed to be highly readable using English keywords. Python is interpreted at runtime and does not require compilation. It supports both procedural and object-oriented programming. Python is beginner friendly and supports a wide range of applications. It is portable, extensible, and has a large standard library. Variables are dynamically typed and support integers, floating point numbers, complex numbers, strings, lists, tuples and dictionaries.
Python can be used for a variety of applications including web development, scientific computing, education, desktop GUIs, and software development. It is commonly used to build web applications using frameworks like Django and Flask, for scientific computing tasks using libraries like NumPy and SciPy, and for general software development tasks like build automation and testing. Python supports a range of data types including integers, floats, complex numbers, lists, dictionaries, sets, and strings. It can be used to write functions and programs to solve problems across many domains.
Basic concept of Python.pptx includes design tool, identifier, variables.supriyasarkar38
油
This document discusses Python programming concepts including data types, variables, operators, and functions. It provides examples of Python syntax for writing and executing code as well as built-in data types like strings, integers, and lists. Key concepts covered include variables, data type casting, comments, arithmetic and comparison operators, and functions.
This document provides information about Jupyter Notebook, including:
- Jupyter Notebook is an open-source web application for creating and sharing documents containing live code, equations, visualizations, and narrative text.
- It works locally on localhost port 8888 and the easiest way to install it is through Anaconda which includes Jupyter Notebook and popular Python libraries.
- Notebooks use kernels to run code in different programming languages, with IPython being the default Python kernel.
¥ 艶COPY & PASTE LINK https://crack4pro.net/download-latest-windows-softwaresz/
Wondershare Filmora 14.3.2 Crack is an excellent software for video editing. It is beneficial in perfect video editing with industry standards.
AnyDesk Pro 3.7.0 Crack License Key Free Download 2025 [Latest]haroonsaeed605
油
copy and past on google 油ぬも https://mediiafiire.com/
"AnyDesk Pro: Secure, fast, and reliable remote desktop software for professionals. Access and control devices from anywhere with low latency, high performance, and top-tier security. Perfect for businesses and IT teams. Try Any Desk Pro today!"
Advance Website Helpdesk Customer Support Ticket Management OdooAagam infotech
油
Effortlessly manage tickets via email, admin, or website forms, and take advantage of features like merging, reopening, and assigning by type. Create orders and invoices directly from tickets, send updates via WhatsApp, and track progress with timesheetseverything you need, all in one place.
Get the App here : https://bit.ly/4fJjGm8
Key features of advanced website helpdesk odoo module :
Dashboard For Tickets Tracking
Helpdesk Tickets List
Helpdesk Tickets Filter
advanced helpdesk SLA Policy
Assign Tickets Via Ticket Type, Team
Helpdesk Multi language support
And more.....
Just visit our app link and explore more new interesting features of Advanced website helpdesk odoo module
App download now :
Odoo 18 : https://bit.ly/4fJjGm8
Odoo 17 : https://bit.ly/3tEBcWg
Odoo 16 : https://bit.ly/3FEH6K6
Odoo 15 : https://bit.ly/3yTpJ4H
Odoo 14 : https://bit.ly/3ywuIbj
Odoo 13 : https://bit.ly/3rIXMZ8
Ask us for free Demo ? business@aagaminfotech.com
Want to discuss: http://www.aagaminfotech.com
Explore more odoo Apps: https://bit.ly/3y02ofI
Click this link to download NOW : https://shorturl.at/zvrcM
Tenorshare 4uKey Crack is a versatile software tool designed to help users bypass or remove various types of passwords and locks from iOS devices. It's primarily used to recover access to locked iPhones, iPads, or iPods without the need for a password or Apple ID. This software is particularly helpful when users forget their screen lock passcode, Face ID, Touch ID, or Apple ID password. It supports a wide range of iOS devices and works with various versions of iOS, making it a useful tool for iOS users in need of password recovery.
ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?Ava Isley
油
This PPT on "ChatGPT and DeepSeek: Which AI Tool Delivers Better User Experience?" provides a comparative analysis of the two AI tools, focusing on usability, interface design, customization, and overall user satisfaction.
It evaluates their strengths and weaknesses to determine which offers a superior experience.
For businesses seeking custom software development services in USA, choosing the right AI tool is essential for enhancing automation, customer interaction, and workflow efficiency.
AI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber ScaleAlluxio, Inc.
油
AI/ML Infra Meetup
Mar. 06, 2025
Organized by Alluxio
For more Alluxio Events: https://www.alluxio.io/events/
Speaker:
- Sean Po (Staff SWE @ Uber)
- Tse-Chi Wang (Senior SWE @ Uber)
This talk provided a deep dive into how Uber manages its Generative AI Gateway, which powers all generative AI applications across the company.
iTop VPN Latest Version 2025 Crack Free Downloadlr74xqnvuf
油
Click this link to download NOW : https://shorturl.at/zvrcM
iTop VPN Latest Version 2025 Crack is a popular VPN (Virtual Private Network) service that offers privacy, security, and anonymity for users on the internet. It provides users with a way to protect their online activities from surveillance, bypass geo-restrictions, and enhance their security while browsing the web.
Online Software Testing Training Institute in Delhi NcrHome
油
Geekonik is a chief Online Computer program Testing Preparing Founded in Delhi NCR, advertising expert-led courses in manual testing, computerization testing (Selenium, Appium), API testing, and execution testing. Our preparing incorporates hands-on ventures, real-world case ponders, and certification programs, guaranteeing industry-ready abilities. With adaptable online classes, personalized mentorship, and work situation help, we offer assistance you construct a fruitful career in program testing. Connect Geekonik nowadays and improve your specialized skill with cutting-edge program testing methodologies!
Click this link to download NOW : https://shorturl.at/zvrcM
Enscape Latest 2025 Crack is a real-time 3D rendering and virtual reality (VR) software that integrates seamlessly with architectural design software like Autodesk Revit, SketchUp, Rhino, ArchiCAD, and Vectorworks. It is widely used by architects, designers, and visualization professionals to create photorealistic visualizations, immersive virtual walkthroughs, and high-quality renderings directly from their 3D models.
AI/ML Infra Meetup | How Uber Optimizes LLM Training and FinetuneAlluxio, Inc.
油
AI/ML Infra Meetup
Mar. 06, 2025
Organized by Alluxio
For more Alluxio Events: https://www.alluxio.io/events/
Speaker:
- Chongxiao Cao (Senior SWE @ Uber)
Chongxiao Cao from Uber's Michelangelo training team shared valuable insights into Uber's approach to optimizing LLM training and fine-tuning workflows.
艶COPY & PASTE LINK https://crack4pro.net/download-latest-windows-softwaresz/
Free Download Autodesk Revit for Windows PC. It includes features for architectural design, MEP and structural engineering, and construction. Revit supports a multidiscipline, collaborative design process.
Instagram Feed Snippet, Instagram posts display in odoo websiteAxisTechnolabs
油
¥Instagram snippet Odoo module come with Completely responsive and mobile ready, layout looks great on screen, simple way to set up, display photos and thumbnail, display #instagram posts, increase your number of follwers exciting features
Visit Odoo 18 app link : https://bit.ly/3YMgiA3
Let's Checkout Some interesting key features of Odoo instagram Snippet :
Key features of Instagram Odoo Snippet :
Easy to Setup
Any Instagram Profile
Instagram UI Post
Fully Responsive instagram snippet in odoo
Faster Load More
And more....
Just click On below Odoo Instagram Snippet link and explore more exciting new features :
App download now :
Odoo 18 : https://bit.ly/3YMgiA3
Odoo 17 : https://bit.ly/4aiiZ0g
Odoo 16 : https://bit.ly/3WGPzCv
Odoo 15 : https://bit.ly/3LD8N6m
Odoo 14 : https://bit.ly/3K9wL8H
Odoo 13 : https://bit.ly/3DCiW0c
Explore more odoo Apps : https://bit.ly/3oFIOCF
Want A Free DEMO ? : business@axistechnolabs.com
Want to discuss ? : https://lnkd.in/gfXHqhU4
Looking Odoo services : https://lnkd.in/gjxHC4dd
Contact Us : 091066 49361
Why Hire Python Developers? Key Benefits for Your BusinessMypcot Infotech
油
Python developers bring expertise in building scalable, secure, and high-performance applications. They enhance productivity with clean, efficient code, ensuring faster development and seamless integration. With strong community support and versatility across industries, they drive innovation and cost-effective solutions. Boost your business growthhire Python developers today!
For more information please visit here https://www.mypcot.com/hire-python-developer
Why Every Cables and Wires Manufacturer Needs a Cloud-Based ERP SolutionsAbsolute ERP
油
Investing in the right direction with Enterprise Resource Planning Software helps
businesses build a strong base. In this direction, cloud-enabled ERP solutions have become the
call of every manufacturing industry including the cables and wires industry.
Adobe InDesign Crack Full Version Free Download 2025sannnasaba545
油
艶COPY & PASTE LINK https://crack4pro.net/download-latest-windows-softwaresz/
Free download Adobe InDesign CC Pre-activated offline installer for Windows PC. It has everything you need to make posters, books, digital magazines, eBooks, interactive PDFs, etc.
copy and past on google も https://drfiles.net/
SketchUp Pro Crack provides professionals with the tools to create detailed and accurate 3D models, visualize concepts, and communicate ideas effectively.SketchUp Pro, free and safe download. SketchUp Pro latest version: Explore boundless design possibilities with SketchUp Pro. Dive into the world of Sk.SketchUp Pro Crack With License Key 2025. SketchUp is a popular 3D modeling software used for a wide range of applications,
Rise of the Phoenix: Lesson Learned Build an AI-powered Test Gen Enginestevebrudz1
油
In this talk, I give an overview and demo of Phoenix, an AI-powered test generation engine for Ruby on Rails applications, and share lessons learned while building it. I presented this at the Artificial Ruby Meet Up in NYC on March 4, 2025.
2. Housekeeping
This is a whistle-stop tour.
Focus on the more common aspects of Python programming.
Today is intended to be the first of a two-part workshop:
- Today: Absolute basics of Python.
- Workshop on 06/11/2019: Data Analysis and visualisation with
Python.
Learning a language is all about practice.
Google is your best friend.
3. An introduction to coding with Python
An introductory coding course with Python
4. Interpretive vs compiled languages
Python is an interpretive language.
This means that your code is not directly run by the hardware. It is
instead passed to a virtual machine, which is just another programme
that reads and interprets your code. If your code used the +
operation, this would be recognised by the interpreter at run time,
which would then call its own internal function add(a,b), which would
then execute the machine code ADD.
This is in contrast to compiled languages, where your code is
translated into native machine instructions, which are then directly
executed by the hardware. Here, the + in your code would be
translated directly in the ADD machine code.
5. Advantages of Python?
Because Python is an interpretive
language, it has a number of
advantages:
Automatic memory management.
Expressivity and syntax that is English.
Ease of programming.
Minimises development time.
Python also has a focus on importing
modules, a feature that makes it
useful for scientific computing.
6. Disadvantages
Interpreted languages are slower than compiled languages.
The modules that you import are developed in a decentralised
manner; this can cause issues based upon individual assumptions.
Multi-threading is hard in Python
7. Which language is the best
No one language is
better than all others.
The best language
depends on the task
you are using it for and
your personal
preference.
8. Versions of Python
There are currently two versions of Python in use; Python 2 and
Python 3.
Python 3 is not backward compatible with Python 2.
A lot of the imported modules were only available in Python 2 for
quite some time, leading to a slow adoption of Python 3. However,
this not really an issue anymore.
Support for Python 2 will end in 2020.
9. The Anaconda IDE
The Anaconda distribution is the most popular Python
distribution out there.
Most importable packages are pre-installed.
Offers a nice GUI in the form of Spyder.
Before we go any further, lets open Spyder:
11. Variables
Variables in python can contain alphanumerical characters and some
special characters.
By convention, it is common to have variable names that start with
lower case letters and have class names beginning with a capital
letter; but you can do whatever you want.
Some keywords are reserved and cannot be used as variable names
due to them serving an in-built Python function; i.e. and, continue, break.
Your IDE will let you know if you try to use one of these.
Python is dynamically typed; the type of the variable is derived from
the value it is assigned.
12. Variable types
Integer (int)
Float (float)
String (str)
Boolean (bool)
Complex (complex)
[]
User defined (classes)
A variable is assigned using the = operator; i.e:
In: Out:
Create an integer, float, and string variable.
Print these to the screen.
Play around using different variable names,
etc.
The print() function is used to print something
to the screen.
13. You can always check the type of a variable using the type() function.
In: Out:
Check the type of one of your variables.
15. Arithmetic operators
The arithmetic operators:
Addition: +
Subtract: -
Multiplication: *
Division: /
Power: **
Write a couple of operations
using the arithmetic operators,
and print the results to the
screen.
In: Out:
16. A quick note on the increment operator
shorthand
Python has a common idiom that is not necessary, but which is used frequently
and is therefore worth noting:
x += 1
Is the same as:
x = x + 1
This also works for other operators:
x += y # adds y to the value of x
x *= y # multiplies x by the value y
x -= y # subtracts y from x
x /= y # divides x by y
17. Boolean operators
Boolean operators are useful when making conditional statements,
we will cover these in-depth later.
and
or
not
18. Comparison operators
Greater than: >
Lesser than: <
Greater than or equal to: >=
Lesser than or equal to: <=
Is equal to: ==
In:
Out:
Write a couple of operations using
comparison operators; i.e.
19. In: Out:
Working with strings
Create a string variable.
Work out the length of the string.
21. Indexing
Indexing in Python is 0-based, meaning that the first element in a
string, list, array, etc, has an index of 0. The second element then has
an index of 1, and so on.
In: Out:
You can cycle backwards through a list, string, array, etc, by placing a
minus symbol in front of the index location.
In: Out:
22. In: Out:
In: Out:
Create a string that is 10 characters in length.
Print the second character to the screen.
Print the third to last character to the screen.
Print all characters after the fourth character.
Print characters 2-8.
23. Tuples
Tuples are containers that are immutable; i.e. their contents cannot
be altered once created.
In: Out:
In: Out:
24. Lists
Lists are essentially containers
of arbitrary type.
They are probably the container
that you will use most
frequently.
The elements of a list can be of
different types.
The difference between tuples
and lists is in performance; it is
much faster to grab an
element stored in a tuple, but
lists are much more versatile.
Note that lists are denoted by []
and not the () used by tuples.
In:
Out:
Create a list and populate it with
some elements.
25. Lists are mutable; i.e. their contents can be changed. This can be
done in a number of ways.
With the use of an index to replace a current element with a new
one.
In: Out:
Adding elements to a list
Replace the second element in your string with the integer 2.
26. You can use the insert() function in order to add an element to a list at
a specific indexed location, without overwriting any of the original
elements.
In: Out:
Use insert() to put the integer 3 after the 2 that you just added to your
string.
27. You can add an element to the end of a list using the append() function.
In: Out:
Use append() to add the string end as the last element in your list.
28. Removing elements from a list
You can remove an element from a list based upon the element value.
Remember: If there is more than one element with this value, only
the first occurrence will be removed.
In: Out:
29. It is better practice to remove elements by their index using the del
function.
In: Out:
Use del to remove the 3 that you added to the list earlier.
30. For loops
The for loop is used to iterate over elements in a sequence, and is
often used when you have a piece of code that you want to repeat a
number of times.
For loops essentially say:
For all elements in a sequence, do something
31. An example
We have a list of species:
The command underneath the list then cycles through each entry in the species list
and prints the animals name to the screen. Note: The i is quite arbitrary. You could
just as easily replace it with animal, t, or anything else.
32. Another example
We can also use for loops for operations other than printing to a
screen. For example:
Using the list you made a moment ago, use a for loop to print each
element of the list to the screen in turn.
33. The range() function
The range() function generates a list of numbers, which is generally used to iterate
over within for loops.
The range() function has two sets of parameters to follow:
range(stop)
stop: Number of integers
(whole numbers) to generate,
starting from zero. i.e:
range([start], stop[, step])
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step: Difference between each number in the sequence
i.e.:
Note:
All parameters must be integers.
Parameters can be positive or negative.
The range() function (and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to
access the first element of a list is mylist[0]. Therefore the last integer generated by range() is up to, but not including,
stop.
34. Create an empty list.
Use the range() and append() functions to add the integers 1-20 to
the empty list.
Print the list to the screen, what do you have?
Output:
35. The break() function
To terminate a loop, you can use the break() function.
The break() statement breaks out of the innermost enclosing for or while
loop.
36. The continue() statement is used to tell Python to skip the rest of the
statements in the current loop block, and then to continue to the next
iteration of the loop.
The continue () function
37. While loops
The while loop tells the computer to do something as long as a
specific condition is met.
It essentially says:
while this is true, do this.
When working with while loops, its important to remember the
nature of various operators.
While loops use the break() and continue() functions in the same way as a
for loop does.
40. Create a variable and set it to zero.
Write a while loop that states that, while the variable is less than 250,
add 1 to the variable and print the variable to the screen.
Replace the < with <=, what happens?
In: Out:
41. For loop vs. while loop
You will use for loops more often than while loops.
The for loop is the natural choice for cycling through a list, characters
in a string, etc; basically, anything of determinate size.
The while loop is the natural choice if you are cycling through
something, such as a sequence of numbers, an indeterminate number
of times until some condition is met.
42. Nested loops
In some situations, you may want a loop within a loop; this is known
as a nested loop.
In:
Out:
What will the code on the right
produce?
Recreate this code and run it, what do
you get?
43. Conditionals
There are three main conditional statements in Python; if, else, elif.
We have already used if when looking at while loops.
In: Out:
In: Out:
45. Functions
A function is a block of code which
only runs when it is called.
They are really useful if you have
operations that need to be done
repeatedly; i.e. calculations.
The function must be defined
before it is called. In other words,
the block of code that makes up
the function must come before the
block of code that makes use of
the function.
In:
Out:
46. In:
Out:
Create a function that takes two inputs, multiplies them, and then returns the result. It should
look some like:
def function_name(a, b):
do something
return something
Create two different lists of integers.
Using your function, write a nested for loop that cycles through each entries in the first list and
multiples it by each of the entries in the second list, and prints the result to the screen.
48. Reading and writing to files in Python: The file
object
File handling in Python can easily be done with the built-in object file.
The file object provides all of the basic functions necessary in order to
manipulate files.
Open up notepad or notepad++. Write some text and save the file
to a location and with a name youll remember.
49. The open() function
Before you can work with a file, you first have to open it using Pythons in-built
open() function.
The open() function takes two arguments; the name of the file that you wish to use
and the mode for which we would like to open the file
By default, the open() function opens a file in read mode; this is what the r above
signifies.
There are a number of different file opening modes. The most common are: r=
read, w=write, r+=both reading and writing, a=appending.
Use the open() function to read the file in.
50. The close() function
Likewise, once youre done working with a file, you can close it with the close()
function.
Using this function will free up any system resources that are being used up by
having the file open.
51. Reading in a file and printing to screen
example
Using what you have now learned about for loops, it is possible to open
a file for reading and then print each line in the file to the screen using
a for loop.
Use a for loop and the variable name that you assigned the open file to in
order to print each of the lines in your file to the screen.
In: Out:
52. The read() function
However, you dont need to use any loops to access file contents.
Python has three in-built file reading commands:
1. <file>.read() = Returns the entire contents of the file as a single string:
2. <file>.readline() = Returns one line at a time:
3. <file>.readlines() = Returns a list of lines:
53. The write() function
Likewise, there are two similar in-built functions for getting Python to
write to a file:
1. <file>.write() = Writes a specified sequence of characters to a file:
2. <file>.writelines() = Writes a list of strings to a file:
Important: Using the write() or writelines() function will overwrite anything
contained within a file, if a file of the same name already exists in the working
directory.
54. Practice writing to a file in Python
Part 1:
Open the file you created in the last practice and ready it for being written to.
Write a string to that file. Note: this will overwrite the old contents.
Remember to close the file once you are done.
Part 2:
Create a list of strings.
Use the open() function to create a new .txt file and write your list of strings to
this file.
Remember to close the file once you are done.
55. The append() function
If you do not want to overwrite a files contents, you can use the append() function.
To append to an existing file, simply put a instead of r or w in the open() when opening
a file.
56. Practice appending to a file in Python
Open the text file you created in part two of the writing to a file practice, and ready it
for appending.
Define a string object.
Appending this new string object to the file.
Remember to close the file once you are done.
57. A word on import
To use a package in your code, you must first make it accessible.
This is one of the features of Python that make it so popular.
In:
There are pre-built Python packages for pretty much everything.
In:
58. Plotting in Python
Before creating an plots, it is worth spending sometime familiarising
ourselves with the matplotlib module. It will save a lot of time later on.
59. Some history.
Matplotlib was originally developed by a neurobiologist in order to
emulate aspects of the MATLAB software.
The pythonic concept of importing is not utilised by MATLAB, and this
is why something called Pylab exists.
Pylab is a module within the Matplotlib library that was built to mimic
the MATLAB style. It only exists in order to bring aspects of NumPy and
Matplotlib into the namespace, thus making for an easier transition for
ex-MATLAB users, because they only had to do one import in order to
access the necessary functions:
However, using the above command is now considered bad practice,
and Matplotlib actually advises against using it due to the way in which it
creates many opportunities for conflicted name bugs.
60. Getting started
Without Pylab, we can normally get away with just one canonical
import; the top line from the example below.
We are also going to import NumPy, which we are going to use to
generate random data for our examples.
61. Different graph types
A simple line graph can be plotted with plot().
A histogram can be created with hist().
A bar chart can be created with bar().
A pie chart can be created with pie().
A scatter plot can be created with scatter().
The table() function adds a text table to an axes.
Plus many more.
62. Our first plot
You may be wondering why the x-axis ranges from
0-3 and the y-axis from 1-4.
If you provide a single list or array to the plot()
command, Matplotlib assumes it is a sequence of
y values, and automatically generates the x values
for you.
Since python ranges start with 0, the default x
vector has the same length as y but starts with 0.
Hence the x data are [0,1,2,3].
63. The plot() function
The plot() argument is quite versatile,
and will take any arbitrary collection
of numbers. For example, if we add an
extra entry to the x axis, and replace
the last entry in the Y axis and add
another entry:
64. The plot() function
The plot() function has an optional third argument that specifies the
appearance of the data points.
The default is b-, which is the blue solid line seen in the last two
examples. The full list of styles can be found in the documentation for the
plot() on the Matplotlib page
65. The plot() function
You can quite easily alter the properties of the line with the plot() function.
66. Altering tick labels
The plt.xticks() and plt.yticks()
allows you to manually
alter the ticks on the x-
axis and y-axis
respectively.
Note that the tick values
have to be contained
within a list object.
67. Practice - Basic line graph
Lets write a Python program to draw a line graph with suitable labels
for the x-axis and y-axis. Include a title.
68. The setp() function
The setp() allows you to set multiple properties for a list of lines, if you want
all the lines to be matching.
You can use the setp() function along with either the line or lines function in order to get
a list of settable line properties.
69. The axis() function
The axis() function allows us to specify the range of the axis.
It requires a list that contains the following:
[The min x-axis value, the max x-axis value, the min y-axis, the max y-axis value]
70. Matplotlib and NumPy arrays
Normally when working with numerical data, youll be using NumPy
arrays.
This is still straight forward to do in Matplotlib; in fact all sequences are
converted into NumPy arrays internally anyway.
71. Working with text
There are a number of different ways in which to
add text to your graph:
- title() = Adds a title to your graph,
takes a string as an argument
- xlabel() = Add a title to the x-axis, also
takes a string as an argument
- ylabel() = Same as xlabel()
- text() = Can be used to add text to an
arbitrary location on your graph.
Requires the following arguments:
text(x-axis location, y-axis location, the string
of text to be added)
Matplotlib uses TeX equation expressions. So, as an
example, if you wanted to put in one of the
text blocks, you would write plt.title(r'$sigma_i=15$').
72. Annotating data points
The annotate() function allows you to easily annotate data points or
specific area on a graph.
73. Legends
The location of a legend is specified by
the loc command. There are a number
of in-built locations that can be altered
by replacing the number. The
Matplotlib website has a list of all
locations in the documentation page
for location().
You can then use the bbox_to_anchor()
function to manually place the legend,
or when used with loc, to make slight
alterations to the placement.
74. Saving a figure as a file
The plt.savefig() allows you to
save your plot as a file.
It takes a string as an
argument, which will be the
name of the file. You must
remember to state which
file type you want the figure
saved as; i.e. png or jpeg.
Make sure you put the
plt.savefig() before the
plt.show() function.
Otherwise, the file will be a
blank file.
75. Scatter plot exercise
Lets write a Python program to plot quantities which have an x and y
position; a scatter graph.
76. Debugging
Debugging is in fundamental aspect of coding, and you will probably spend more
time debugging than actually writing code.
EVERYONE has to debug, it is nothing to be ashamed of.
In fact, you should be particularly concerned if you do write a programme that
does not display any obvious errors, as it likely means that you are just unaware
of them.
There are a number of debugging programmes available to coders. However,
debugging the most common issues that youll encounter when developing
programmes can be done by following a few key principles.
However, always remember that sometimes fixing a
bug can create new bugs.
77. Print everything
When debugging, the most important function at your disposal is the
print command. Every coder uses this as a debugging tool, regardless
of their amount of experience.
You should have some sense as to what every line of code you have
written does. If not, print those lines out. You will then be able to see
how the values of variables are changing as the programme runs
through.
Even if you think you know what each line does, it is still
recommended that you print out certain lines as often this can aid
you in realising errors that you may have overlooked.
78. Print examples
I want the value of variable to be 10 upon
completion of the for loop. Did the for loop work
correctly?
Yes, it did.
Did this chunk of code run?
No.
79. Run your code when you make changes
Do not sit down and code for a hour or so without running the code
you are writing. Chances are, you will never get to the bottom of all of
the errors that your programme reports when it runs.
Instead, you should run your script every few minutes. It is not
possible to run your code too many times.
Remember, the more code you write or edit between test runs, the
more places you are going to have to go back an investigate when
your code hits an error.
80. Read your error messages
Do not be disheartened when you get an error message. More often
than not, youll realise what the error is as soon as you read the
message; i.e. the for loop doesnt work on a list because the list is
empty.
This is particularly the case with Python, which provides you with
error messages in clear English compared to the cryptic messages
given by offered by other languages.
At the very least, the error message will let you know which lines is
experiencing the error. However, this may not be the line causing the
error. Still, this offers a good starting point for your bug search.
81. Google the error message
This can sometimes be a bit of a hit-or-miss, depending on the nature of the error.
If your error is fairly specific, then there will nearly always be a webpage where
someone has already asked for help with an error that is either identical or very
similar to the one you are experiencing; stackoverflow.com is the most common page
youll come across in this scenario.
Do make sure that you read the description
of the problem carefully to ensure that the
problem is the same as the one you are
dealing with. Then read the first two or
three replies to see if page contains a
workable solution.
If you cannot work out the cause of an error
message, google the error code and description.
82. Comment out code
You can often comment out bits of code that are not related to the
chunk of code that contains the error.
This will obviously make the code run faster and might make it easier
to isolate the error.
83. Binary searches
This method draws upon a lot of the methods we have already
covered.
Here, you want to break the code into chunks; normally two chunks,
hence this methods name.
You then isolate which chunk of code the error is in.
After which, you take the chunk of code in question, and divide that
up, and work out which of these new chunks contains the error.
So on until youve isolate the cause of the error.
84. Walk away
If you have been trying to fix an error for a prolonged period of time,
30 minutes or so, get up and walk away from the screen and do
something else for a while.
Often the answer to your issue will present itself upon your return to
the computer, as if by magic.
85. Phrase your problem as a question
Many software developers have been
trained to phrase their problem as a
question.
The idea here is that phrasing your issue in
this manner often helps you to realise the
cause of the problem.
This often works!
86. Ask someone
If all else fails, do not hesitate to ask a colleague or friend who is a
coder and maybe familiar with the language for help.
They may not even need to be a specialist, sometimes a fresh pair of
eyes belonging to someone who is not invested in the project is more
efficient at helping you work out your issue than spending hours
trying to solve the issue on your own or getting lost the internet
trying to find a solution.
88. Useful resources
There are two great online resources for learning this language
through practical examples. These are the Code Academy
(https://www.codecademy.com/catalog/subject/web-development)
and Data Camp
(https://www.datacamp.com/?utm_source=adwords_ppc&utm_cam
paignid=805200711&utm_adgroupid=39268379982&utm_device=c&
utm_keyword=data%20camp&utm_matchtype=e&utm_network=g&
utm_adpostion=1t1&utm_creative=230953641482&utm_targetid=k
wd-
298095775602&utm_loc_interest_ms=&utm_loc_physical_ms=10067
07&gclid=EAIaIQobChMI3o2iqtbV2wIVTkPTCh2QRA19EAAYASAAEgLZ
dPD_BwE).