PYTHON Basics-List Tuple Dictionar and loops (1).pptx

Full Transcript

PYTHON BASICS <INSERT YOUR NAME> PYTHON DATA TYPES Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview None Type: NoneType LIST • Lists are use...

PYTHON BASICS <INSERT YOUR NAME> PYTHON DATA TYPES Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview None Type: NoneType LIST • Lists are used to store multiple items in a single variable. • Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. • Lists are created using square brackets: • myList = ["apple", “dell", “hp"] print(myList) • List items are ordered, changeable, and allow duplicate values. To determine how many items a list has, use the len() function: LIST • myList = ["apple", “dell", “hp"] print(len(myList)) --len() method to check the length • List items can be of any data type • print(type(myList)) --type() method to check type of data • It is also possible to use the list() constructor when creating a new list • myList = list(("apple", “dell", “hp“)) To determine how many items a list has, use the len() function: ACCESSING ITEMS IN LIST • myList = ["apple", “dell", “hp“,”huwaei] print(myList[1]) • print(myList[2:3]) • myList.insert(2, “asus") • myList.append(“ibm") • myList.remove(“ibm") • myList.sort() • myList.sort(reverse = True) TUPLES • Tuples are used to store multiple items in a single variable. • Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. • A tuple is a collection which is ordered and unchangeable. • Tuples are written with round brackets. • thistuple = ("apple", "banana", "cherry") print(thistuple) TUPLES • thistuple = ("apple",) print(type(thistuple)) • It is also possible to use the tuple() constructor to make a tuple. • thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets print(thistuple) • print(thistuple[1]) • thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(thistuple[2:5]) TUPLES • thistuple = ("apple", "banana", "cherry") if "apple" in thistuple: print("Yes, 'apple' is in the fruits tuple") • Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created. SET • Sets are used to store multiple items in a single variable. • A set is a collection which is unordered, unchangeable*, and unindexed. • thisset = {"apple", "banana", "cherry"} print(thisset) • Once a set is created, you cannot change its items, but you can remove items and add new items. • You cannot access items in a set by referring to an index or a key. • You can iterate using for loop DICTIONARY • Dictionaries are used to store data values in key:value pairs. • A dictionary is a collection which is ordered*, changeable and do not allow duplicates. • thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict) • print(thisdict["brand"]) DICTIONARY • Dictionaries are used to store data values in key:value pairs. • A dictionary is a collection which is ordered*, changeable and do not allow duplicates. • thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict) • print(thisdict["brand"]) DICTIONARY • thisdict = dict(name = "John", age = 36, country = "Norway") print(thisdict) • x = thisdict[“name"] • x = thisdict.get(“name") • x = thisdict.keys() FUNCTIONS • A function is a block of code which only runs when it is called. • You can pass data, known as parameters, into a function. • A function can return data as a result. • In Python a function is defined using the def keyword: • def my_function(): print("Hello from a function") FUNCTIONS • Calling a function: • To call a function, use the function name followed by parenthesis: • def my_function(): print("Hello from a function") my_function() FUNCTIONS ARGUMENTS • Information can be passed into functions as arguments. • Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. • def my_function(fname): print(fname + " Refsnes") my_function("Emil") my_function("Tobias") my_function("Linus") FUNCTIONS ARGUMENTS • The terms parameter and argument can be used for the same thing: information that are passed into a function. • By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less. • def my_function(fname, lname): print(fname + " " + lname) my_function("Emil", "Refsnes") FUNCTIONS ARGUMENTS • def my_function(fname, lname): print(fname + " " + lname) my_function("Emil") • ??? Will it work?? FUNCTIONS ARBITARY ARGUMENTS • If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition. • This way the function will receive a tuple of arguments, and can access the items accordingly: • def my_function(*kids): print("The youngest child is " + kids[2]) my_function("Emil", "Tobias", "Linus") FUNCTIONS KEYWORD ARGUMENTS • You can also send arguments with the key = value syntax. • This way the order of the arguments does not matter. • def my_function(child3, child2, child1): print("The youngest child is " + child3) my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus") PASSING LIST AS ARGUMENT • You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function. • E.g. if you send a List as an argument, it will still be a List when it reaches the function: • def my_function(food): for x in food: print(x) fruits = ["apple", "banana", "cherry"] my_function(fruits) RETURN VALUES • To let a function return a value, use the return statement: • def my_function(x): return 5 * x print(my_function(3)) print(my_function(5)) print(my_function(9)) • function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error. RETURN VALUES • def myfunction(): pass • factorial = 1 • # check if the number is negative, positive or zero • if num < 0: • print("Sorry, factorial does not exist for negative numbers") • elif num == 0: • print("The factorial of 0 is 1") • else: • for i in range(1,num + 1): • factorial = factorial*i • print("The factorial of",num,"is",factorial)

Use Quizgecko on...
Browser
Browser