0% found this document useful (0 votes)
51 views57 pages

Lecture 1 Introduction To Python Python

Python learn from zero to Advanced web developer Ai game Pandas numpy sprinklers flask Jupiter py for
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views57 pages

Lecture 1 Introduction To Python Python

Python learn from zero to Advanced web developer Ai game Pandas numpy sprinklers flask Jupiter py for
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Advance Programming

Lecture 1 and 2 :Introduction To python

Edited by Dr. Akram


Alhammadi
Advance Programming
• Introduction and features
• If Condition and loop
• File Handling
• Class /Objects (OOP)
• Networking
• GUI
• Database
• Anaconda and Jupyter Notebook
• Optional if we have time
1. Numpy
2. panda
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.
• Python is an interpreted language, which means the source code of a Python
program is converted into bytecode that is then executed by the Python virtual
machine. You do not need to compile your program before executing it. This is
similar to PERL and PHP.
• Python is Interactive: This means that you can actually sit at a Python prompt and
interact with the interpreter directly to write your programs.
• garbage-collected automatically free up memory space that has been allocated to
object no longer needed by the program.
• Dynamic typing means that the type of the variable is determined only during
runtime.
• 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).
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
• Software engineer
• Many more other roles
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 )
○ 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
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 float Numbers with a decimal point: 2.3 4.6 100.0
point
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()
• X.count('o')
• X.capitalize()
• X.lower()
• X.split() output is [‘hello’, ‘worlds’]
• X.split(‘o’)
○ 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.
• d.keys()
• d.values()
• d.items()
• del(d)
• d.clear()
• del d[‘key’]
Tuples
Tuples are very similar to lists. However they have one
key difference - immutability.
Once an element is inside a tuple, it can not be
reassigned.
Tuples use parenthesis: (1,2,3)
A Python tuple consists of a number of values separated by commas.
Unlike lists, however, tuples are enclosed within parentheses.
• cannot be updated. Tuples can be thought of as read-only lists
• len(t)
• type(t)
• # Use .index to enter a value and return the index
• t.index('one')
• # Use .count to count the number of times a value appears
• t.count('one')
• t[0]= 'change‘ ((Immutability))
• del t
• max(t)
• min(t)
• t.append('nope') ((AttributeError ))
Sets
Sets are unordered collections of unique elements.
Meaning there can only be one representative of the
same object.
• x = set()
• # We add to sets with the add() method
• x.add(1)
• x.add(1)
• # Create a list with repeats
• list1 = [1,1,2,2,3,4,5,6,1,1]
• # Cast as set to get unique values
• M=set(list1)
Python Data Type Conversion

• Sometimes, you may need to perform conversions between the built-in


data types. To convert data between different Python data types, you
simply use the type name as a function.
• b = int(2.2)
• a = float(1)
• b = str(2.2)
• tuple(s)
• list(s)
• set(s)
• dict(d)……………….

You might also like