Docsity

Corso di Python Basics, Sintesi del corso di Programmazione Orientata agli Oggetti

Anteprima parziale del testo

Scarica Corso di Python Basics e più Sintesi del corso in PDF di Programmazione Orientata agli Oggetti solo su Docsity!

PI Python Basics ov € Navigation Basics & Setup Command Line Basics {Windows): Running Python Code on CMD No Install Options for Notebooks Types of Integrated Development Environments (IDE) Execute Shell Commands from within an Environment Python Object and Data Structure Basics Objects & Data Structure types Creating Object & Data Structure Numbers 1. Basic Arithmetic: 2. Variable Assignments: Lambda Expressions, Map, and Filter Installing Packages, Modules & Libraries in python Package Installer Pip Basics & Setup Command Line Basics (Windows): w CODE: # show the current directory cd # List files and folders in the current directory dir # Change the current directory (hit 'Tab' to Autocomplete) cd # Move back from the current directory cd .. # Clear the console screen cls # Copy files or directories copy # Delete files del # Create a new directory mkdir # Remove a directory (empty directories only) rmdir # Rename a file or directory ren # Display the contents of a text file type Running Python Code on CMD w CODE: # First Change directry where the file located using folling command cd # Then to run file as python type ‘python ' python , then type name of the file (use 'Tab' to Autocomplete) OR # run folling command to directly type code on the command line python # use to quit quit() No Install Options for Notebooks * jupyterorg/try e Google Collab Notebook e Replit (Paid) e Google Search © “Python Interpreters Online” Types of Integrated Development Environments (IDE) e Text Editors o Sublime Text o Atom o VSCode e Full IDE o PyCharm o Spyder e Notebook Environments o Jupyter Notebook o Google Collab Notebook Execute Shell Commands from within an Environment In a command-line interface or terminal, the ‘ : ‘ symbol is used to execute shell commands from within the environment where the Python interpreter is running. For example ipip install /?|\\0I@#$%A&*=-+ 4. It's considered best practice (PEP8) that names are lowercase. 5. Avoid using the characters "l' (lowercase letter el), ‘0' (uppercase letter oh), or 'l' (uppercase letter eye) as single character variable names. 6. Avoid using words that have special meaning in Python like "list" and "str" w CODE: ata >>> 10 # Reassignment a=10 # Use A to redefine A a=at+a >>> 20 # Deleting a variable assignment using del keyword. a=20 print(a) >>> 20 del a # here the 'a' is no longer assigned to some value. print(a) >>> NameError: name 'a' is not defined Strings 1. Creating & Printing Strings v CODE: # Strings can be created by single quote or double quote "String built with double quotes" "I'm using double quotes" s[1] >>> e We can use slicing to grab a subsection from a object. slicing syntax object[start:stop:step] # Slicing # We can use a : to perform slicing which grabs everything up to a designated point. # Grab everything past the first term all the way to the length of s which is len(s) s[1:] >>> 'ello World' # Grab everything UP TO the 3rd index, 3rd index is not included. s[:3] >>> 'Hel' #Everything s[:] >>> ‘Hello World' # Negative Indexing # Last letter s[-1] >>> 'd' # Grab everything but the last letter s[:-1] >>> 'Hello Worl' # Step Size # Grab everything, but go in steps size of 1 s[::1] >>> "Hello World' # Grab everything, but go in step sizes of 2 s[::2] >>> 'HloWrd' # We can use this to print a string backwards s[::-1] >>> 'dlroW olleH' 3. String Properties w CODE: s = 'Hello World concatenate me!" # Upper Case a string s.upper() # Lower case s.lower() # Split a string by blank space (this is the default) s.split() >>> ['Hello', 'World', 'concatenate', 'mel'] # Split by a specific element (doesn't include the element that was split on) s.split('W') >>> ['Hello ', 'orld concatenate me!'] # There are many more methods than the ones covered here. 5. Print Formatting v CODE # 1.String Formatting with % Operator: name = "John" age = 25 print("My name is %s and I am %d years old." % (name, age)) # %s is a placeholder for a string (used for the variable name). # %d or %i is a placeholder for an integer (used for the variable age). # %f: Floating-point decimal. # %.nf: Floating-point decimal with n digits after the decimal point. # %x or %X: Hexadecimal representation (lowercase or uppercase, respectively). # 2.str.format() Method: name = "John" age = 25 print("My name is {} and I am {} years old.".format(name, age)) #0R print("My name is {n} and I am {a} years old.".format(n=name, a=age)) # 3.Formatted F-String Literals: name = "John"