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

Py Fun Pre

Uploaded by

Anvitha Moilla
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)
19 views

Py Fun Pre

Uploaded by

Anvitha Moilla
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/ 34

# Declare a variable and initialize it

f = 0
print(f)
# re-declaring the variable works
f = 'guru99‘
print(f)
Keywords in python 3

False class finally is return


None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Comments
A comment is a piece of text within a program that is not executed. It can be used to provide
additional information to aid in understanding the code.
The # character is used to start a comment and it continues until the end of the line.
# Comment on a single line

user = "JDoe" # Comment after code

print() Function
The print() function is used to output text, numbers, or other printable information to the
console.
It takes one or more arguments and will output each of the arguments to the console separated
by a space. If no arguments are provided, the print() function will output a blank line.

print("Hello World!")
print(100)
pi = 3.14159
print(pi)
Strings
A string is a sequence of characters (letters, numbers, whitespace or punctuation) enclosed by
quotation marks. It can be enclosed using either the double quotation mark " or the single
quotation mark '.
If a string has to be broken into multiple lines, the backslash character \ can be used to indicate
that the string continues on the next line.

user = "User Full Name"


game = 'Monopoly'
longer = "This string is broken up \
over multiple lines"

Variables
A variable is used to store data that will be used by the program. This data can be a number, a
string, a Boolean, a list or some other data type. Every variable has a name which can consist
of letters, numbers, and the underscore character _.
The equal sign = is used to assign a value to a variable. After the initial assignment is made,
the value of a variable can be updated to new values as needed.

# These are all valid variable names and assignment


user_name = "@sonnynomnom"
user_id = 100 verified = False # A variable's value can be
changed after assignment
points = 100 points = 120
Integers # Example integer numbers
An integer is a number that can be written chairs = 4
without a fractional part (no decimal). An tables = 1
integer can be a positive number, a negative broken_chairs = -2
number or the number 0 so long as there is no sofas = 0
decimal portion. # Non-integer numbers
The number 0 represents an integer value but lights = 2.5
the same number written as 0.0 would left_overs = 0.0
represent a floating point number.

Floating Point Numbers


Python variables can be assigned different
types of data. One supported data type is the # Floating point numbers
floating point number. A floating point pi = 3.14159
number is a value that contains a decimal meal_cost = 12.99
portion. It can be used to represent numbers tip_percent = 0.20
that have fractional quantities. For example, a
= 3/5 can not be represented as an integer, so
the variable a is assigned a floating point
value of 0.6.
•variables are referred to "envelop" or "buckets" where information can be maintained and
referenced. Like any other programming language Python also uses a variable to store the
information.
•Variables can be declared by any name or even alphabets like a, aa, abc, etc.
•Variables can be re-declared even after you have declared them for once
•In Python you cannot concatenate string with number directly, you need to declare them as a
separate variable, and after that, you can concatenate number with string
•Declare local variable when you want to use it for current function
•Declare Global variable when you want to use the same variable for rest of the program
•To delete a variable, it uses keyword "del".
Arithmetic Operations
# Arithmetic operations
Python supports different types of arithmetic
operations that can be performed on literal
result = 10 + 30
numbers, variables, or some combination. The
result = 40 - 10
primary arithmetic operators are:
result = 50 * 5
+ for addition
result = 16 / 4
- for subtraction
result = 25 % 2
* for multiplication
result = 5 ** 3
/ for division
% for modulus (returns the
remainder)
** for exponentiation

Modulo Operator %
A modulo calculation returns the remainder of # Modulo operations
a division between the first and second number.
For example: zero = 8 % 4
The result of the expression 4 % 2 would result
in the value 0, because 4 is evenly divisible by nonzero = 12 % 5
2 leaving no remainder.
The result of the expression 7 % 3 would return
1, because 7 is not evenly divisible by 3,
leaving a remainder of 1.
String Concatenation
Python supports the joining (concatenation)
of strings together using the + operator. #String concatenation
The + operator is also used for mathematical first = "Hello "
second = "World"
addition operations. If the parameters passed
result = first + second
to the + operator are strings, then long_result = first + second + "!"
concatenation will be performed. If the
parameter passed to + have different types,
then Python will report an error condition.
Multiple variables or literal strings can be
joined together using the + operator.
Plus-Equals Operator +=
The plus-equals operator += provides a convenient way to add a value to an existing variable
and assign the new value back to the same variable. In the case where the variable and the
value are strings, this operator performs string concatenation instead of addition.
The operation is performed in-place, meaning that any other variable which points to the
variable being updated will also be updated.

#Plus-Equal Operator
counter = 0
counter += 10 # This is equivalent to counter = 0
counter = counter + 10 #The operator will also perform string concatenation
message = "Part 1 of message"
message += "Part 2 of message"
Functions
Some tasks need to be performed multiple times within a program. Rather than rewrite the
same code in multiple places, a function may be defined using the def keyword. Function
definitions may include parameters, providing data input to the function.
Functions may return a value using the return keyword followed by the value to return.

# Define a function my_function() with parameter x


def my_function(x):
return x + 1 # Invoke the function

print(my_function(2)) # Output: 3
print(my_function(3 + 5)) # Output: 9
Calling Functions
Python uses simple syntax to use, invoke, or call a preexisting function. A function can be
called by writing the name of it, followed by parentheses.
For example, the code provided would call the doHomework() method.

doHomework()
Function Indentation
Python uses indentation to identify blocks of code. Code within the same block should be
indented at the same level. A Python function is one type of code block. All code under a
function declaration should be indented to identify it as part of the function. There can be
additional indentation within a function to handle other statements such as for and if so long
as the lines are not indented less than the first line of the function code.
#Indentation is used to identify code blocks
def testfunction(number): # This code is part of testfunction
print("Inside the testfunction")
sum = 0
for x in range(number):
# More indentation because 'for' has a code block
# but still part of the function
sum += x
return sum
print("This is not part of testfunction")
Function Parameters
Sometimes functions require input to provide data for their code. This input is defined
using parameters.
Parameters are variables that are defined in the function definition. They are assigned the
values which were passed as arguments when the function was called, elsewhere in the code.
For example, the function definition defines parameters for a character, a setting, and a skill,
which are used as inputs to write the first sentence of a book.

def write_a_book(character, setting, special_skill):


print(character + " is in " + setting + " practicing her " + special_skill)

Multiple Parameters
Python functions can have multiple parameters. Just as you wouldn’t go to school without
both a backpack and a pencil case, functions may also need more than one input to carry out
their operations.
To define a function with multiple parameters, parameter names are placed one after
another, separated by commas, within the parentheses of the function definition.

def ready_for_school(backpack, pencil_case):


if (backpack == 'full' and pencil_case == 'full'):
print ("I'm ready for school!")
Function Arguments
Parameters in python are variables — placeholders for the actual values the function needs.
When the function is called, these values are passed in as arguments.
For example, the arguments passed into the function .sales() are the “The Farmer’s Market”,
“toothpaste”, and “$1” which correspond to the parameters grocery_store, item_on_sale,
and cost.

def sales(grocery_store, item_on_sale, cost):


print(grocery_store + " is selling " + item_on_sale + " for " +
cost)

sales("The Farmer’s Market", "toothpaste", "$1")


Function Keyword Arguments
Python functions can be defined with named arguments which may have default values
provided. When function arguments are passed using their names, they are referred to as
keyword arguments. The use of keyword arguments when calling a function allows the
arguments to be passed in any order — not just the order that they were defined in the
function. If the function is invoked without a value for a specific argument, the default value
will be used.

def findvolume(length=1, width=1, depth=1):


print("Length = " + str(length))
print("Width = " + str(width))
print("Depth = " + str(depth))
return length * width * depth;

findvolume(1, 2, 3)
findvolume(length=5, depth=2, width=4)
findvolume(2, depth=3, width=4)
Returning Value from Function
A return keyword is used to return a value from a Python function. The value returned from a
function can be assigned to a variable which can then be used in the program.
In the example, the function check_leap_year returns a string which indicates if the passed
parameter is a leap year or not.

def check_leap_year(year):
if year % 4 == 0:
return str(year) + " is a leap year."
else:
return str(year) + " is not a leap year."

year_to_check = 2018
returned_value = check_leap_year(year_to_check)
print(returned_value) # 2018 is not a leap year.
Returning Multiple Values
Python functions are able to return multiple values using one return statement. All values that
should be returned are listed after the return keyword and are separated by commas.
In the example, the function square_point() returns x_squared, y_squared, and z_squared.

def square_point(x, y, z):


x_squared = x * x
y_squared = y * y
z_squared = z * z # Return all three values:
return x_squared, y_squared, z_squared

three_squared, four_squared, five_squared = square_point(3, 4,


5)
Parameters as Local Variables
Function parameters behave identically to a function’s local variables. They are initialized with
the values passed into the function when it was called.
Like local variables, parameters cannot be referenced from outside the scope of the function.
In the example, the parameter value is defined as part of the definition of my_function, and
therefore can only be accessed within my_function. Attempting to print the contents
of value from outside the function causes an error.

def my_function(value):
print(value)

# Pass the value 7 into the function


my_function(7) # Causes an error as `value` no longer exists
print(value)
Global Variables
A variable that is defined outside of a function is called a global variable. It can be accessed
inside the body of a function.
In the example, the variable a is a global variable because it is defined outside of the
function prints_a. It is therefore accessible to prints_a, which will print the value of a.

a = "Hello"
def prints_a():
print(a)

# will print "Hello"

prints_a()
The Scope of Variables
In Python, a variable defined inside a function is called a local variable. It cannot be used
outside of the scope of the function, and attempting to do so without defining the variable
outside of the function will cause an error.
In the example, the variable a is defined both inside and outside of the function. When the
function f1() is implemented, a is printed as 2 because it is locally defined to be so. However,
when printing a outside of the function, a is printed as 5 because it is implemented outside of
the scope of the function.

a = 5
def f1():
a = 2
print(a)

print(a) # Will print 5


f1() # Will print 2
Equal Operator ==
The equal operator, ==, is used to compare two values, variables or expressions to determine if
they are the same.
If the values being compared are the same, the operator returns True, otherwise it
returns False.
The operator takes the data type into account when making the comparison, so a string value
of "2" is not considered the same as a numeric value of 2.
# Equal operator
if 'Yes' == 'Yes':
# evaluates to True
print('They are equal')

if (2 > 1) == (5 < 10):


# evaluates to True
print('Both expressions give the same result')

c = '2'
d = 2

if c == d:
print('They are equal')
else:
print('They are not equal')
Not Equals Operator !=
The Python not equals operator, !=, is used to compare two values, variables or expressions to
determine if they are NOT the same. If they are NOT the same, the operator returns True. If
they are the same, then it returns False.
The operator takes the data type into account when making the comparison so a value
of 10 would NOT be equal to the string value "10" and the operator would return True. If
expressions are used, then they are evaluated to a value of True or False before the comparison
is made by the operator.

# Not Equals Operator


if "Yes" != "No":
# evaluates to True
print("They are NOT equal")

val1 = 10
val2 = 20
if val1 != val2:
print("They are NOT equal")

if (10 > 1) != (10 > 1000):


# True != False
print("They are NOT equal")
Comparison Operators
In Python, relational operators compare two values or expressions. The most common ones
are:
< less than
> greater than
<= less than or equal to
>= greater than or equal too
If the relation is sound, then the entire expression will evaluate to True. If not, the expression
evaluates to False.

a = 2
b = 3
a < b # evaluates to True
a > b # evaluates to False
a >= b # evaluates to False
a <= b # evaluates to True
a <= a # evaluates to True
and Operator
The Python and operator performs a Boolean True and True # Evaluates to True
comparison between two Boolean values,
True and False # Evaluates to False
variables, or expressions. If both sides of the
operator evaluate to True then False and False # Evaluates to False
the and operator returns True. If either side
(or both sides) evaluates to False, then 1 == 1 and 1 < 2 # Evaluates to True
the and operator returns False. A non-
Boolean value (or variable that stores a value) 1 < 2 and 3 < 1 # Evaluates to False
will always evaluate to True when used with
"Yes" and 100 # Evaluates to True
the and operator.

True or True # Evaluates to True


or Operator
The Python or operator combines two True or False # Evaluates to True
Boolean expressions and evaluates
False or False # Evaluates to False
to True if at least one of the expressions
returns True. Otherwise, if both expressions 1 < 2 or 3 < 1 # Evaluates to True
are False, then the entire expression
evaluates to False. 3 < 1 or 1 > 6 # Evaluates to False

1 == 1 or 1 < 2 # Evaluates to True


not Operator
not True # Evaluates to False
The Python Boolean not operator is
not False # Evaluates to True
used in a Boolean expression in order to
1 > 2 # Evaluates to False
evaluate the expression to its inverse
Not 1 > 2 # Evaluates to True
value. If the original expression
1 == 1 # Evaluates to True
was True, including the not operator
not 1 == 1 # Evaluates to False
would make the expression False, and
vice versa.

if Statement
The Python if statement is used to # if Statement
determine the execution of code based
on the evaluation of a Boolean test_value = 100
expression. if test_value > 1:
If the if statement expression evaluates # Expression evaluates to True
to True, then the indented code print("This code is executed!")
following the statement is executed.
if test_value > 1000:
If the expression evaluates # Expression evaluates to False
to False then the indented code print("This code is NOT executed!")
following the if statement is skipped and
the program executes the next line of print("Program continues at this point.")
code which is indented at the same level
as the if statement.
else Statement # else Statement
The Python else statement provides test_value = 50
alternate code to execute if the expression if test_value < 1:
in an if statement evaluates to False. print("Value is < 1")
The indented code for the if statement is else:
executed if the expression evaluates print("Value is >= 1")
to True. The indented code immediately
following the else is executed only if the test_string = "VALID"
expression evaluates to False. To mark the if test_string == "NOT_VALID":
end of the else block, the code must be print("String equals
unintended to the same level as the NOT_VALID")
starting if line. else: print("String equals
something else!")
Boolean Values
Booleans are a data type in Python, much like
integers, floats, and strings. However, is_true = True
booleans only have two values: is_false = False
True
False print(type(is_true)) # will
Specifically, these two values are of output: <class 'bool'>
the bool type. Since booleans are a data type,
creating a variable that holds a boolean value
is the same as with other data types.
elif Statement
The Python elif statement allows for continued checks to be performed after an
initial if statement. An elif statement differs from the else statement because another
expression is provided to be checked, just as with the initial if statement.
If the expression is True, the indented code following the elif is executed. If the
expression evaluates to False, the code can continue to an optional else statement.

Multiple elif statements can be used following an initial if to perform a series of checks.
Once an elif expression evaluates to True, no further elif statements are executed.

# elif Statement
pet_type = "fish"
if pet_type == "dog":
print("You have a dog.")
elif pet_type == "cat":
print("You have a cat.")
elif pet_type == "fish":
# this is performed
print("You have a fish")
else:
print("Not sure!")
Handling Exceptions in Python
A try and except block can be used to handle error in code block. Code which may raise an error
can be written in the try block. During execution, if that code block raises an error, the rest of
the try block will cease executing and the except code block will execute.

def check_leap_year(year):
is_leap_year = False
if year % 4 == 0:
is_leap_year = True

try:
check_leap_year(2018)
print(is_leap_year) # The variable is_leap_year is declared inside
the function
except:
print('Your code raised an error!')
Lists
In Python, lists are ordered collections of items that
allow for easy use of a set of data.
primes = [2, 3, 5, 7, 11]
List values are placed in between square
brackets [ ], separated by commas. It is good
print(primes)
practice to put a space between the comma and the
empty_list = []
next value. The values in a list do not need to be
unique (the same value can be repeated).
Empty lists do not contain any values within the
square brackets.

Python Lists: Data Types


In Python, lists are a versatile data type that
numbers = [1, 2, 3, 4, 10]
can contain multiple different data types
names = ['Jenny', 'Sam', 'Alexis']
within the same square brackets. The mixed = ['Jenny', 1, 2]
possible data types within a list include list_of_lists = [['a', 1], ['b', 2]]
numbers, strings, other objects, and even
other lists.
List Method .append()
In Python, you can add values to the end of a orders = ['daisies', 'periwinkle']
list using the .append() method. This will orders.append('tulips')
place the object passed in as a new element at print(orders)
the very end of the list. Printing the list # Result:
afterwards will visually show the appended
value. This .append() method is not to be ['daisies', 'periwinkle', 'tulips']
confused with returning an entirely new list
with the passed object.

Adding Lists Together


In Python, lists can be added to each other using the plus symbol +. As shown in the code block,
this will result in a new list containing the same items in the same order with the first list’s items
coming first.
Note: This will not work for adding one item at a time (use .append() method). In order to add
one item, create a new list with a single value and then use the plus symbol to add the list.

items = ['cake', 'cookie', 'bread']


total_items = items + ['biscuit', 'tart']
print(total_items)

# Result:
['cake', 'cookie', 'bread', 'biscuit', 'tart']
Determining List Length with len()
The Python len() function can be used to determine the number of items found in the list it
accepts as an argument.

knapsack = [2, 4, 3, 7, 10]


size = len(knapsack)
print(size)

# Output: 5

Zero-Indexing
In Python, list index begins at zero and ends at the length of the list minus one. For example, in
this list, 'Andy' is found at index 2.
names = ['Roger', 'Rafael', 'Andy', 'Novak']

List Indices
Python list elements are ordered by index, a number referring to their placement in the list. List
indices start at 0 and increment by one.
To access a list element by index, square bracket notation is used: list[index].

berries = ["blueberry", "cranberry", "raspberry"]


berries[0] # "blueberry"
berries[2] # "raspberry"
List Slicing
A slice, or sub-list of Python list elements can be selected from a list using a colon-separated
starting and ending point.
The syntax pattern is myList[START_NUMBER:END_NUMBER]. The slice will include
the START_NUMBER index, and everything until but excluding the END_NUMBER item.
When slicing a list, a new list is returned, so if the slice is saved and then altered, the original
list remains the same.
tools = ['pen', 'hammer', 'lever']
tools_slice = tools[1:3] # ['hammer', 'lever']
tools_slice[0] = 'nail'

# Original list is unaltered:


print(tools) # ['pen', 'hammer', 'lever']

List Item Ranges Including First or Last Item


In Python, when selecting a range of list items, if the first item to be selected is at index 0, no
index needs to be specified before the :. Similarly, if the last item selected is the last item in the
list, no index needs to be specified after the :.
items = [1, 2, 3, 4, 5, 6]

# All items from index `0` to `3`


print(items[:4])
# All items from index `2` to the last item, inclusive
print(items[2:])
Negative List Indices
Negative indices for lists in Python can be used to reference elements in relation to the end of a
list. This can be used to access single list elements or as part of defining a list range.
For instance:
To select the last element, my_list[-1].
To select the last three elements, my_list[-3:].
To select everything except the last two elements, my_list[:-2].
soups = ['minestrone', 'lentil', 'pho', 'laksa']
soups[-1] # 'laksa'
soups[-3:] # 'lentil', 'pho', 'laksa'
soups[:-2] # 'minestrone', 'lentil'
List Method .count()
The .count() Python list method searches a list for whatever search term it receives as an
argument, then returns the number of matching entries fou

backpack = ['pencil', 'pen', 'notebook', 'textbook', 'pen', 'highlighter', 'pen']


numPen = backpack.count('pen')
print(numPen) # Output: 3
List Method .sort()
The .sort() Python list method will sort the contents of whatever list it is called on. Numerical
lists will be sorted in ascending order, and lists of Strings will be sorted into alphabetical order. It
modifies the original list, and has no return value.

exampleList = [4, 2, 1, 3]
exampleList.sort()
print(exampleList)

# Output: [1, 2, 3, 4]
sorted() Function
The Python sorted() function accepts a list as an argument, and will return a new, sorted list
containing the same elements as the original. Numerical lists will be sorted in ascending order,
and lists of Strings will be sorted into alphabetical order. It does not modify the original,
unsorted list.

unsortedList = [4, 2, 1, 3]
sortedList = sorted(unsortedList)
print(sortedList)

# Output: [1, 2, 3, 4]
Python For Loop
A Python for loop can be used to iterate for <temporary variable> in <list variable>:
over a list of items and perform a set of <action statement>
actions on each item. The syntax of <action statement>
a for loop consists of assigning a #each num in nums will be printed below
temporary value to a variable on each
successive iteration. nums = [1,2,3,4,5]
for num in nums:
When writing a for loop, remember to
print(num)
properly indent each action, otherwise
an IndentationError will result.

Python for Loops


Python for loops can be used to iterate over
and perform an action one time for each dog_breeds = ["boxer", "bulldog", "shiba inu"]
element in a list.
Proper for loop syntax assigns a temporary # Print each breed:
value, the current item of the list, to a for breed in dog_breeds:
variable on each successive iteration: for print(breed)
<temporary value>in <a list>:
for loop bodies must be indented to avoid
an IndentationError.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy