BASICS OF Python PROGRAMMING LANGUAGE 🔴🔴🔴




BASICS OF PYTHON PROGRAMMING LANGUAGE

BASICS OF Python PROGRAMMING LANGUAGE

Python ANACONDA SETUP INSTALLATION

Let's install Python the Anaconda in right way:
  • Download Anaconda (which includes Python): https://www.anaconda.com/download/

  • Run the installer and follow the installation instructions

  • Run the Spyder editor and create your first Python program "helloworld.py"

Python SETUP

Python VARIABLES DATA AND TYPES

Variables store and give names to data values. Data values can be of various types:
• int : -5, 0, 1000000
• float : -2.0, 3.14159
• bool : True, False
• str : "Hello world!", "K3WL"
• list : [1, 2, 3, 4], ["Hello", "world!"], [1, 2, "Hello"], [ ]
• And many more!
• In Python, variables do not have types!
• Data values are assigned to variables using "="
x = 1 # this is a Python comment
x = x + 5
y = "Python" + " is " + "cool!"

Python CONDITIONALS AND LOOPS:

• Rely on boolean expressions which return either True or False
• 1 < 2 : True
• 1.5 >= 2.5 : False
• answer == "Computer Science" : can be True or False depending on the value of variable answer
• Boolean expressions can be combined with: and, or, not
• 1 < 2 and 3 < 4 : True
• 1.5 >= 2.5 or 2 == 2 : True • not 1.5 >= 2.5 : True

Python CONDITIONALS: GENERIC FORM

if boolean-expression-1:
code-block-1
elif boolean-expression-2:
code-block-2
(as many elif's as you want)
else:
code-block-last

Python CONDITIONALS: GRADING

grade = int(raw_input("Numeric grade: "))
if grade >= 80:
print("A")
elif grade >= 65:
print("B")
elif grade >= 55:
print("C")
else: print("E")

Python LOOPS

Python loops are useful for repeating code:
while boolean-expression:
code-block

for element in collection:
code-block

Python WHITE LOOPS

while raw_input("Which is the best subject? ") != "Computer Science": print("Try again!")
print("Of course it is!"

Python FOR LOOPS

We have seen (briefly) two kinds of collections: string and list
For loops can be used to visit each collection's element:
for chr in "string": print(chr)
for elem in [1,3,5]: print(elem)

Python FUNCTIONS

Functions encapsulate code blocks

PYTHON FUNCTIONS: GENERIC FORM

def function-name(parameters):
code-block
return value

Python FUNCTIONS: CELSIUS TO FAHRENHEIT

def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 1.8 + 32.0
return fahrenheit
Python FUNCTIONS: DEFAULT AND NAMED PARAMETERS
def hello(name_man="Bro", name_woman="Sis"):
print("Hello, " + name_man + " & " + name_woman + "!")

hello()
Hello, Bro & Sis!
>>> hello(name_woman="Lady")
Hello, Bro & Lady!
>>> hello(name_woman="Angeline",name_man="Tom") Hello, Tom & Angeline!

Python IMPORTS

Two ways of importing modules:
• Generic form: import module_name
import math
print(math.sqrt(4))
• Generic form: from module_name import function_name
from math import sqr
print(sqrt(4))

Python STRING

String is a sequence of characters, like "Python is cool". Each character has an index:

Each character has an index
• Accessing a character: string[index]
x = "Python is cool"
print(x[10])
• Accessing a substring via slicing: string[start:finish]
print(x[2:6])

Python STRING OPERATIONS

x = "Python is cool"
>>> "cool" in x # membership
>>> len(x) # length of string x
>>> x + "?" # concatenation
>>> x.upper() # to upper case
>>> x.replace("c", "k") # replace characters in a string

Python STRING OPERATIONS SPLIT


Python STRING OPERATIONS SPLIT
x = "Python is cool"
>>> y = x.split(" ")
>>> ",".join(y)

Python LISTS

• If a string is a sequence of characters, then
a list is a sequence of items!
• List is usually enclosed by square brackets [ ]
• As opposed to strings where the object is fixed (= immutable),
we are free to modify lists (that is, lists are mutable)
x = [1, 2, 3, 4]
x[0] = 4
x.append(5)
print(x) # [4, 2, 3, 4, 5]

Python LIST OPERATIONS

>>> x = [ "Python", "is", "cool" ]
>>> x.sort() # sort elements in x
>>> x[0:2] # slicing>>> len(x) # length of string x
>>> x + ["!"] # concatenation
>>> x[2] = "hot" # replace element at index 0 with "hot"
>>> x.remove("Python") # remove the first occurrence of "Python"
>>> x.pop(0) # remove the element at index 0

Python TUPLES
• Like a list, but you cannot modify it (= immutable)
• Tuple is usually (but not necessarily) enclosed by parentheses ()
• Everything that works with lists, works with tuples,
except functions modifying the tuples' content
• Example
x = (0,1,2)
y = 0,1,2 # same as x
x[0] = 2 # this gives an error

Python SETS

• As opposed to lists, in sets duplicates are removed and
there is no order of elements!
• Set is of the form { e1, e2, e3, … }
• Operations include: intersection, union, difference.
• Example:
x = [0,1,2,0,0,1,2,2]
y = {0,1,2,0,0,1,2,2}
print(x)
print(y)
print(y & {1,2,3}) # intersection
print(y | {1,2,3}) # union
print(y - {1,2,3}) # difference

Python CLASSES

While in functions we encapsulate a set of instructions, in classes we encapsulate objects! A class is a blueprint for objects, specifying:
• Attributes for objects
• Methods for objects
• A class can use other classes as a base •

Python CLASSES GENERIC FORM

class class-name(base):
attribute-code-block
method-code-block

KEEP LEARNING PYTHON!

https://docs.python.org Python Official Documentation

https://stackoverflow.com/questions/tagged/python Python on stackoverflow

No comments:

Post a Comment