Full Transcript

Unit 1 Getting started with Python Language What is Python ▪ Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. ▪ What is Python? ▪ Python is a high-level, general-purpose,...

Unit 1 Getting started with Python Language What is Python ▪ Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. ▪ What is Python? ▪ Python is a high-level, general-purpose, and very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. ▪ Python language is being used by almost all tech-giant companies like – Google, Amazon, Facebook, Instagram, Dropbox, Uber… etc. ▪ The biggest strength of Python is huge collection of standard library which can be used for the following: Machine Learning GUI Applications (like Kivy, Tkinter, PyQt etc. ) Web frameworks like Django (used by YouTube, Instagram, Dropbox) Image processing (like OpenCV, Pillow) Web scraping (like Scrapy, BeautifulSoup, Selenium) Test frameworks Multimedia Scientific computing Text processing and many more.. Why Learn Python? ▪ Python is currently the most widely used multi-purpose, high-level programming language, which allows programming in Object-Oriented and Procedural paradigms. Python programs generally are smaller than other programming languages like Java. Programmers have to type relatively less and the indentation requirement of the language, makes them readable all the time. Features ▪ of python: Python's features include- ▪ Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows a student to pick up the language quickly. ▪ Easy-to-read: Python code is more clearly defined and visible to the eyes. ▪ Easy-to-maintain: Python's source code is fairly easy-to-maintain. ▪ A broad standard library: Python's bulk of the library is very portable and cross platform compatible on UNIX, Windows, and Macintosh. ▪ Interactive Mode: Python has support for an interactive mode, which allows ▪ interactive testing and debugging of snippets of code. ▪ Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms. ▪ Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient. ▪ Databases: Python provides interfaces to all major commercial databases. ▪ GUI Programming: Python supports GUI applications that can be created and ported Variables ▪ Variables ▪ Variables are containers for storing data values. ▪ Creating Variables ▪ Python has no command for declaring a variable. ▪ A variable is created the moment you first assign a value to it. ▪x = 5 y = "John" print(x) print(y) In each case the result is the value of the variable. Variables also have types; again, we can ask the interpreter what they are. Variable names and keywords: A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) A variable name cannot be any of the Python keywords.. ▪ Camel Case ▪ Eg: myVariableName = "John“ ▪ Pascal Case ▪ Eg: MyVariableName = "John“ ▪ Snake Case ▪ Each word is separated by an underscore character: ▪ Eg: my_variable_name = "John" Keywords: Keywords define the language’s rules and structures, and they cannot be used as variable names. python has thirty-one keywords. Type conversion ▪ Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type-name as a function. ▪ There are several built-in functions to perform conversion from one data type to another. ▪ int(x [,base])- Converts x to an integer. The base specifies the base if x is a string. ▪ float(x)- Converts x to a floating-point number. ▪ str(x)- Converts object x to a string representation. ▪ Tuple(x)-converts to a tuple ▪ List(s)-converts to a list ▪ dict(d)- Creates a dictionary. d must be a sequence of (key,value) tuples. Comments ▪ Comments can be used to explain Python code. ▪ Comments can be used to make the code more readable. Comments starts with a #, and Python will ignore them: #This is a comment print("Hello, World!") Multiline Comments """ This is a comment written in more than just one line """ print("Hello, World!") Python Data Types ▪ In programming, data type is an important concept. ▪ Variables can store data of different types, and different types can do different things. ▪ Python has the following data types built-in by default, in these categories: ▪ Numeric values are stored in numbers. The whole number, float, and complex qualities have a place with a Python Numbers datatype. Python offers the type() function to determine a variable's data type. ▪ Example: a=5 print("The type of a", type(a)) b = 40.5 print("The type of b", type(b)) c = 1+3j print("The type of c", type(c)) output Sequence Type ❑ String ▪ The sequence of characters in the quotation marks can be used to describe the string. A string can be defined in Python using single, double, or triple quotes. ▪ Example: x = str("Hello World") #display x: print(x) #display the data type of x: print(type(x)) Output → ❑ List ▪ Lists in Python are like arrays in C, but lists can contain data of different types. The things put away in the rundown are isolated with a comma (,) and encased inside square sections []. ▪ Example list1 = [1, "hi", "Python", 2] #Printing the list1 print (list1) ❑ Tuple ▪ In many ways, a tuple is like a list. Tuples, like lists, also contain a collection of items from various data types. Because we cannot alter the size or value of the items in a tuple, it is a read-only data structure. ▪ Example tup = ("hi", "Python", 2) #Printing the tuple print (tup) ❑ Dictionary ▪ A dictionary is a key-value pair set arranged in any order. It stores a specific value for each key, like an associative array or a hash table. ▪ The comma (,) and the curly braces are used to separate the items in the dictionary. ▪ Example: d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'} # Printing dictionary print (d) ❑ Boolean ▪ True and False are the two default values for the Boolean type. These qualities are utilized to decide the given assertion valid or misleading. ▪ Example ▪ print(type(True)) ▪ print(type(False)) Set ▪ Sets are used to store multiple items in a single variable. ▪ Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. ▪ A set is a collection which is unordered, unchangeable*, and unindexed. ▪ * Note: Set items are unchangeable, but you can remove items and add new items. ▪ Duplicates Not Allowed ▪ Sets cannot have two items with the same value. ▪ Example: thisset = {"apple", "banana", "cherry"} print(thisset) # Note: the set list is unordered, meaning: the items will appear in a random order. ▪ Get the Length of a Set ▪ To determine how many items a set has, use the len() function. ▪ Example: thisset = {"apple", "banana", "cherry"} print(len(thisset)) Output → 3 Example 2: set2 = {'James', 2, 3,'Python'} set2.add(10) print(set2) #Removing element from the set set2.remove(2) print(set2) ▪ The set() Constructor ▪ It is also possible to use the set() constructor to make a set. ▪ Example: thisset = set(("apple", "banana", "cherry")) # note the double round-brackets print(thisset) The clear() method empties the set: Example thisset = {"apple", "banana", "cherry"} thisset.clear() print(thisset) ▪ Add Sets ▪ To add items from another set into the current set, use the update() method. ▪ Example: ▪ thisset = {"apple", "banana", "cherry"} tropical = {"pineapple", "mango", "papaya"} thisset.update(tropical) print(thisset) Join Two Sets ▪ There are several ways to join two or more sets in Python. ▪ You can use the union() method that returns a new set containing all items from both sets, or the update() method that inserts all the items from one set into another: ▪ set1 = {"a", "b" , "c"} set2 = {1, 2, 3} set3 = set1.union(set2) print(set3) ▪ Output → {1, 2, 3, 'b', 'a', ‘c’} Keep ONLY the Duplicates ▪ The intersection_update() method will keep only the items that are present in both sets. x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.intersection_update(y) print(x) Output →{'apple'} Operators and Operands: ▪ Operators are special symbols that represent computations like addition and multiplication. The values the operator uses are called operands. The following are all legal Python expressions whose meaning is more or less clear: ▪ Python divides the operators in the following groups: Arithmetic operators ▪ Example: x=2 y=5 print(x ** y) #same as 2*2*2*2*2 Assignment operators ▪ Assignment operators are used to assign values to variables: ▪ Example: x=5 x += 3 print(x) ▪ 0/p→ 8 Comparison Operators ▪ Comparison operators are used to compare two values: ▪ Example: x=5 y=3 print(x != y) # returns True because 5 is not equal to 3 Output → true Logical Operators ▪ Logical operators are used to combine conditional statements: ▪ Example: x=5 print(not(x > 3 and x < 10)) print(not(x > 3 and x < 10)) # returns False because not is used to reverse the result ▪ Output → false false Bitwise Operators ▪ Bitwise operators are used to compare (binary) numbers: ▪ 1) AND (&) ▪ print(6 & 3) ▪ Output → 2 ▪ The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0: Decimal numbers and their binary values: ▪ 6 = 0000000000000110 0 = 0000000000000000 1 = 0000000000000001 ▪ 3 = 0000000000000011 2 = 0000000000000010 ▪ -------------------- 3 = 0000000000000011 4 = 0000000000000100 ▪ 2 = 0000000000000010 5 = 0000000000000101 ▪ ==================== 6 = 0000000000000110 7 = 0000000000000111 ▪ OR (|) ▪ print(6 | 3) ▪ Output → 7 ▪ The | operator compares each bit and set it to 1 if one or both is 1, otherwise it is set to 0: ▪ XOR (^) Example: ▪ print(6 ^ 3) ▪ Output → 5 ▪ The ^ operator compares each bit and set it to 1 if only one is 1, otherwise (if both are 1 or both are 0) it is set to 0:

Use Quizgecko on...
Browser
Browser