0% found this document useful (0 votes)
25 views

Lecture 1 and 2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lecture 1 and 2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Advance Programming

In Python

Edited by :
Dr. Akram Alhammadi
Advance Programming
• Introduction and features
• If Condition and loop
• File Handling
• Class /Objects (OOP)
• Networking
• GUI
• Database
• Jupyter Notebook
• Anaconda
• Numpy
• Pandas
• Optional (Matplotlib and Seaborn)
Programming in Python

As you learn Python throughout this course, there are a few things you
should keep in mind.
• Python is case sensitive.
• Spacing is important.
• Use error messages to help you learn.
What is Python?

• Python is a very popular general-purpose interpreted, interactive,


object-oriented, and high-level programming language. Python is
dynamically-typed and garbage-collected programming language.
• It was created by Guido van Rossum during 1985- 1990. Like Perl,
Python source code is also available under the GNU General Public
License (GPL).
Python Jobs

• Python is very high in demand and all the major companies


are looking for great Python Programmers to develop
websites, software components, and applications or to work
with Data Science, AI, and ML technologies.
Why to Learn Python?

Python is consistently rated as one of the world's most popular


programming languages. There are many other good reasons which
makes Python as the top choice of any programmer:
• Python is Open Source which means its available free of cost.
• Python is simple and so easy to learn
• Python is versatile(support many platforms) and can be used to create
many different things.
• Python has powerful development libraries include AI, ML etc.
• Python is much in demand and ensures high salary
Careers with Python

Here are just a few of the career options where Python is a key skill:
• Game developer
• Web designer
• Python developer
• Machine learning engineer
• Data scientist
• Data analyst
• Data engineer
• DevOps engineer
• Software engineer
• Many more other roles
What can you do with Python?

○ Data Science and Machine Learning


■ Analyze large data files
■ Create visualizations
■ Perform machine learning tasks
■ Create and run predictive algorithms
Python Features

• Easy-to-learn
• Easy-to-read
• Easy-to-maintain
• A broad standard library
• Portable
• Databases
• GUI Programming
Getting Python

• The most up-to-date and current source code, binaries,


documentation, news, etc., is available on the official website of
Python https://www.python.org/
Python installation
● To install Python we will use the free Anaconda distribution.
● This distribution includes Python as well as many other useful
libraries, including Jupyter.
● Anaconda can also easily be installed on to any major OS, Windows,
MacOS, or Linux.
● There is also Miniconda , which is a smaller sized version of
Anaconda.
● To begin installation go to:
www.anaconda.com/downloads
● There are several ways to run Python code.
● First let’s discuss the various options for development
environments
● There are 3 main types of environments:
○ Text Editors (Most popular: Sublime Text and Atom)
○ Full IDEs(Most popular: PyCharm and Spyder)
○ Notebook Environments (Most popular is Jupyter
Notebook)
● Text Editors
○ General editors for any text file
○ Work with a variety of file types
○ Keep in mind, most are not designed with only Python in mind.
Most popular: Sublime Text and Atom
● Notebook Environments
○ Great for learning.
○ See input and output next to each other.
○ Support in-line markdown notes, visualizations, videos, and
more.
○ Special file formats that are not .py
○ Most popular is Jupyter Notebook.
● Full IDEs
○ Development Environments designed specifically for Python.
○ Larger programs.
○ Only community editions are free.
○ Designed specifically for Python, lots of extra functionality.
Most popular: PyCharm and Spyder
Local Environment Setup
find out if it is already installed and which version is installed.
Running Python

• From commend line


• From text editor
• Print(“Hello World!”)

• X=input(“What is your name”)

• print("Hello "+input("what is your name"))


Python Identifiers
• A Python identifier is a name used to identify a variable, function,
class, module or other object. An identifier starts with a letter A to Z
or a to z or an underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
• Python does not allow punctuation characters such as @, $, and %
within identifiers.
• Python is a case sensitive programming language.
Thus, Manpower and manpower are two different identifiers in
Python.
Here are naming conventions for Python identifiers −
• Python Class names start with an uppercase letter. All other identifiers
start with a lowercase letter.
• Starting an identifier with a single leading underscore indicates that
the identifier is private identifier.
• Starting an identifier with two leading underscores indicates a
strongly private identifier.
Python Reserved Words
Python Lines and Indentation
Python Multi-Line Statements

• Statements in Python typically end with a new line. Python does,


however, allow the use of the line continuation character (\) to
denote that the line should continue. For example −

• Statements contained within the [], {}, or () brackets do not need to


use the line continuation character.
• days = ['Monday', 'Tuesday', 'Wednesday',
Quotations in Python

• Python accepts single ('), double (") and triple (''' or """) quotes to
denote string literals, as long as the same type of quote starts and
ends the string.
• The triple quotes are used to span the string across multiple lines. For
example, all the following are legal
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
Comments in Python
They are added with the purpose of making the source code easier for
humans to understand, and are ignored by Python interpreter.
There are three types of comments available in Python
1. Single line Comments
2. Multiline Comments
3. Docstring Comments
• A hash sign (#) that is not inside a string literal begins a comment. All
characters after the # and up to the end of the physical line are part of
the comment and the Python interpreter ignores them.
Following triple-quoted string is also ignored by Python interpreter and can be used as a multiline comments:

''' This is a multiline


comment. '''
Docstring Comments

• Python docstrings provide a convenient way to provide a help


documentation with Python modules, functions, classes, and
methods. The docstring is then made available via the __doc__
attribute.
Multiple Statements on a Single Line

• The semicolon ( ; ) allows multiple statements on the single line given


that neither statement starts a new code block. Here is a sample snip
using the semicolon

print('''this's my friend''');print('hi')
Python - Variables
• Python variables are the reserved memory locations used to store values with in a
Python Program. This means that when you create a variable you reserve some space in
the memory.
• Based on the data type of a variable, Python interpreter allocates memory and decides
what can be stored in the reserved memory. Therefore, by assigning different data types
to Python variables, you can store integers, decimals or characters in these variables.
• Python variables do not need explicit declaration to reserve memory space or you can
say to create a variable. A Python variable is created automatically when you assign a
value to it. The equal sign (=) is used to assign values to variables.
counter = 100
print (counter)
print(type(counter))
• You can delete the reference to a number object by using the del statement.
del(counter)
Multiple Assignment

• Python allows you to assign a single value to several variables


simultaneously which means you can create multiple variables at a
time.
Python Variable Names
There are certain rules which should be taken care while naming a
Python variable:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number or any special character
like :'",<>/?|\()!@#$%^&*~-+
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Python variable names are case-sensitive which means Name and
NAME are two different variables in Python.
• Python reserved keywords cannot be used naming the variable.
• There can be no spaces in the name, use _ instead.
● Python uses Dynamic Typing
● This means you can reassign variables to different data types.
● This makes Python very flexible in assigning data types, this is
different than other languages that are “Statically-Typed”
my_dogs = 2

my_dogs = [ “Sammy” , “Frankie” ]


Python Local Variable

• Python Local Variables are defined inside a function. We can not


access variable outside the function.
Python Global Variable

• Any variable created outside a function can be accessed within any


function and so they have global scope.
Data Types
Data Types
• Python Data Types are used to define the type of a variable.
• Let’s quickly discuss all of the possible data types, then we’ll go into
more detail about each one!
• type(x)
• Number operation (+,-,*,/,%)
Name Type Description
Integers int Whole numbers, such as: 3 300 200
Floating point float Numbers with a decimal point: 2.3 4.6 100.0
Strings str Ordered sequence of characters: "hello" 'Sammy' "2000" " 楽しい "
Lists list Ordered sequence of objects: [10,"hello",200.3]
Dictionaries dict Unordered Key:Value pairs: {"mykey" : "value" , "name" : "Frankie"}
Tuples tup Ordered immutable sequence of objects: (10,"hello",200.3)
Sets set Unordered collection of unique objects: {"a","b"}
Booleans bool Logical value indicating True or False
Strings
• Strings are sequences of characters, using the syntax of either single quotes or double quotes.
• Because strings are ordered sequences it means we can using indexing to grab sub-sections
of the string.
• Indexing allows you to grab a single character from the string...
These actions use [ ] square brackets and a number index to indicate positions of what you wish
to grab.
Character : h e l l o
Index : 0 1 2 3 4
Reverse Index: 0 -4 -3 -2 -1
Print(Word[2])
Len(word)
● Slicing allows you to grab a subsection of multiple characters, a
“slice” of the string.
● This has the following syntax:
○ [start:stop:step]
● start is a numerical index for the slice start
● stop is the index you will go up to (but not include)
● step is the size of the “jump” you take.
Immutiable
• X=“Hello Worlds”
• X.upper()
• h.count('o')
• h.capitalize()
• h.lower()
• h.split() output is [‘hello’, ‘worlds’]
• h.split(‘o’)
● Let’s explore two methods for this:
○ .format() method
print("Hello {}".format('Sir'))
Hello Sir
print('The {2} {1} {0}'.format('fox','brown','quick'))
print('First Object: {a}, Second Object: {b}, Third Object:
{c}'.format(a=1,b='Two',c=12.3))
○ f-strings (formatted string literals)
name = 'Fred'

print(f"He said his name is {name}.")


Print(str[1:10:2])
Lists
● Lists are ordered sequences that can hold a variety of object types.
● They use [] brackets and commas to separate objects in the list.
○ [1,2,3,4,5]
● Lists support indexing and slicing.
● Lists can be nested and also have a variety of useful methods.
• len(my_list)
• list1.append('append me!')
• list1.pop(0)
• popped_item = list1.pop() last item
• new_list.reverse()
• new_list.sort()
• max(my_list)
• min(my_list)
Booleans
• Booleans are operators that allow you to convey True or False
statements.
Dictionaries
● Previously we saw how lists store objects in an ordered sequence,
dictionaries use a key-value pairing instead.
● This key-value pair allows users to quickly grab objects without
needing to know an index location.
● Dictionaries use curly braces and colons to signify the keys and their
associated values.
{'key1':'value1','key2':'value2'}
● Dictionaries: Objects retrieved by key name.
Unordered and can not be sorted.
● Lists: Objects retrieved by location.
Ordered Sequence can be indexed or sliced.

You might also like