Transcript for:
Getting Started with Python Scripting

[Music] as the chinese proverb says a journey of a thousand miles begins with a single step today's a big day you're taking your first step in your journey to learn how to write scripts in python it's going to be a little challenging at times but really it's not that scary we'll go slow and give you everything you need to fully grasp each concept before we move along in the next few videos we'll discover the fundamental concepts of computer programming you'll learn what a programming language is what scripting is what languages are out there other than python and how this all relates to i.t we'll also have you coding before you know it with small coding exercises we've cooked up to give you hands-on practice with python this will include writing your very first python script but always keep in mind if at any point along the way you feel lost or confused don't panic you can watch the videos as many times as you need to let the concept sink in plus you can ask questions in the discussion forums which is one of the best ways to find extra information and connect with other learners when i was asked to participate in this program it made me think about when i first started to code if i could give that younger version of myself a piece of advice this is what i would tell her it never works the first time seriously as a newbie i expected it all to work like magic i thought that following the rules and getting it right the first time would prove my value as a coder but that's just not true not even the best of the best if you expect to write perfect code on the first shot you're going to be disappointed you hear that younger self try not to feel overwhelmed by the details connecting the dots only comes with experience so best way to learn is to just jump in the truth is everyone learns at their own pace if you already know some of these concepts feel free to skip ahead to the parts that interest you the most if you're starting from scratch take as long as you need for each concept the assessments will be right there waiting for you when you're done and if at any point you start doubting yourself remember even the most advanced programmers started thinking python what's python well we're about to learn all about it so let's dive in next up we'll be doing a rundown of what programming is [Music] at a basic level a computer program is a recipe of instructions that tells your computer what to do when you write a program you create a step-by-step recipe of what needs to be done to complete a task and when your computer executes the program it reads what you wrote and follows your instructions to the letter how nice is that the recipe is written in a code called programming language programming languages are actually similar to human spoken languages since they have a syntax and semantics now if it's been a while since your last grammar class here's a quick refresher on syntax and semantics in a human language syntax is the rules for how a sentence is constructed while semantics refers to the actual meaning of the statements in english sentences generally have both a subject that's a person place or thing and a predicate usually a verb and a statement that explains what the subject is doing let's take the sentence paula loves to program in python as an example in this sentence paula is the subject and loves to program in python is the predicate to form a sentence that others can understand you need to know both the syntax that constructs the sentence and the semantics that gives it meaning the same applies to programming languages in a programming language like python the syntax is the rules for how each instruction is written and the semantics is the effects the instructions have much like spoken languages there are lots of programming languages to choose from each has its own history features and applications but they all share the same fundamental ideas so once you understand the basic concepts in one programming language it becomes much easier to learn another and lastly computers always do exactly what they're told so when you write a program it's important to be super clear about what you want the computer to do learning the syntax and semantics of the programming language you choose will allow you to do just that make sense before we continue let's spend a moment on terminology in the next few videos you'll hear the term script being used a bunch so what's the difference between a script and a program the line between the two can be a bit blurry and in this course we'll use the terms interchangeably in general you can think of scripts as programs with a short development cycle that can be created and deployed rapidly in other words a script is a program that is short simple and can be written very quickly in this course we'll focus on a specific scripting language called python which we'll use to learn the basics of programming we'll learn about the python syntax the rules of how to write a python program and the semantics or meaning of the different pieces involved before we start learning how to code and having you write your first python script let's talk more about what automation is and why it's useful [Music] although we might not realize it we reap the benefits of automation all the time in our daily lives do you ever pay your bills with scheduled payments or use a self-checkout at the grocery store i always set my coffee machine to start brewing before i've even gotten out of bed the promise of fresh coffee makes early mornings way easier automation is the process of replacing a manual step with one that happens automatically take a traffic light for example which continuously regulates the flow of vehicles at an intersection a traffic light requires a human intervention only when it needs repairs or maintenance the automatic regulation of traffic means that humans don't have to stand at the intersection manually signaling when cars should stop or go instead people can concentrate on more complex creative or difficult tasks like focusing on where you're driving what's more traffic lights don't get tired bored or accidentally display a green light when they met red this highlights another benefit of automation consistency let's face it us humans are flawed and sometimes we make mistakes a human performing the same task hundreds of times will never be as consistent as a machine doing the same thing but for all of its advantages automation isn't a solution for every situation some tasks just aren't suited for automation for example they may require a degree of creativity or flexibility that automatic systems can't provide or for more complicated or less frequently executed tasks creating the automation may actually be more effort or cost than it's worth think about when you get a haircut what would it take to automate the actions of cutting hair with a machine the client's height the shape of their head their current hair length and desired hairstyle would all need to be taken into account when designing the automatic system we need to replicate the creativity and skills of a trained specialist along with extensive testing to ensure the client's safety and quality haircut and if you've ever had a bad experience at a hair salon you know quality can be subjective in this case the cost and effort of automation just isn't worth the benefits of an automatic haircut would provide which is why we don't have robot hair stylists not too complex right automation is a powerful tool when used in the right place at the right moment it can save time reduce errors increase consistency and provide a way to centralize solutions and mistakes making them easier to fix throughout this course and in upcoming ones we'll be talking about when it makes sense to apply automation and exactly how you do it eventually knowing when and where to use automation will become automatic for you [Music] working in it a lot of what we do boils down to using a computer to perform a certain task in your job you might create user accounts configure the network install software backup existing data or execute a whole range of other computer-based tasks from day to day back in my first it job i realized that every day i came into work i typed the same three commands to authenticate into systems those credentials timed out every day by design for security reasons so i created a script that would automatically run these commands for me every morning to avoid having to type them myself funny enough the team that monitors anomalous activity discovered my little invention and contacted me to remove it performed by a computer that need to be done multiple times with little variation are really well suited for automation because when you automate a task you avoid the possibility of human errors and reduce the time it takes to do it imagine this scenario your company had a booth at a recent conference and has gathered a huge list of emails from people interested in learning more about your products you want to send these people your monthly email newsletter but some of the people on the list are already subscribed to receive it so how do you make sure everyone receives your newsletter without accidentally sending it to the same person twice well you could manually check each email address one by one to make sure you only add new ones to the list sounds boring and inefficient right it could be and it's also more error prone you might accidentally miss new emails or add emails that were already there or it might get so boring you fall asleep at your desk even your automated coffee machine won't help you out there so what could you do instead you could get the computer to do the work for you you could write a program that checks for duplicates and then adds each new email to the list your computer will do exactly as it's told no matter how many emails there are in the list so it won't get tired or make any mistakes even better once you've written the program you can use the same code in the future situations saving you even more time pretty cool right it gets better think about when you're going to send these emails out if you send them out manually you'll have to send the same email to everybody personalizing the emails would be way too much manual work if instead you use automation to send them you could have the name and company of each person added to the email automatically the result more effective emails without you spending hours inserting names into the text automating tasks allows you to focus on projects that are a better use of your time letting computers do the boring stuff for you learning how to program is the first step to being able to do this if you want to get computers to do the work for you you're in the right place earlier in this video i told you about the first task i ever automated now i want to tell you about the coolest thing i ever automated it was a script that changed a bunch of access permissions for a whole lot of google internal services the script reversed a large directory tree with tons of different files check the file contents and then updated the permissions to the services based on the conditions that i laid out in the script okay i admit i'm a total nerd but i still think it's really cool [Music] in this course we'll use the python programming language to demonstrate basic programming concepts and how to apply them to writing scripts we mentioned that there are a bunch of programming languages out there so why pick python well we chose python for a few reasons first off programming in python usually feels similar to using a human language this is because python makes it easy to express what we want to do with syntax that's easy to read and write check out this example there's a lot to unpack here so don't worry if you don't understand it right away we'll get into the nitty gritty details later in the course but even if you've never seen a line of code before you might be able to guess what this code does it defines a list with names of friends and then creates a greeting for each name in the list now it's your turn to make friends with python try it out and see what happens throughout this course you'll execute python code using your web browser we'll start with some small coding exercises using code blocks just like the one you experimented with later on as you develop your skills you'll work on larger more complex coding exercises using other tools getting good at something takes a whole lot of practice and programming in python is no different we recommend that you practice every example we share in this course on your own if you don't have python installed on your machine no worries you can still practice using an online python interpreter check out the next reading for links to the most popular python interpreters available online now i'm sure you're wondering what the heck is a python interpreter in programming an interpreter is the program that reads and executes code remember how we set a computer program is like a recipe with step-by-step instructions well if your recipe is written in python the python interpreter is the program that reads what's in the recipe and translates it into instructions for your computer to follow eventually you'll want to install python on your computer so you can run it locally and experiment with it as much as you like we'll guide you through how to install python in the upcoming course but you don't have to have it installed to get your first taste of python you can practice with the quizzes we provide and with the online interpreters and codepads that we'll give you links to in the next reading we'll provide a whole bunch of exercises for you but feel free to come up with your own and share them in the discussion forums feel free to get creative this is your chance to show off your new skills [Music] remember how we mentioned that python is simple and easy to use python makes it easy to express the fundamental concepts of programming like data structures and algorithms with easy to read syntax this makes python a great language to use to learn programming and there are other reasons to pick python too python is super popular in the it industry making it one of the most common programming languages used today python isn't new its first version was released by guido van rossum back in 1991 since then the community that develops it has grown and the language has advanced a lot whenever there's a significant change to the semantics or syntax of the language a new major version is released in 2000 python 2 was released in 2008 we got python 3. in this course we'll use python 3.7 which came out in 2018 for many years python was considered a beginner's language and was mostly used for teaching concepts or writing very small simple scripts like in this course but in recent years the adoption of python has grown dramatically one reason for this is that the language has become more powerful it's also because there's more tools available in python for a growing range of applications you can use python to calculate statistics run your ecommerce site process images interact with web services and do a whole host of other tasks python is perfect for automation it lets you automate everyday tasks by writing simple scripts that are easy to understand and easy to maintain that's why python is the language of choice for lots of people working in it support system administration and web development not only that but it's also used in fast-growing areas of i.t like machine learning or data analytics last but not least python is available for download on a wide variety of operating systems like windows linux and mac os and what's more python is so popular in the workplace that if you are currently working in it you've most likely encountered it already and if you're planning her career in it chances are you'll interact with python quite a bit so there's a whole lot of reasons for why python is relevant to today's i.t industry a large part of programming is learning through trial and error and asking questions so if at any point you get stuck don't get discouraged making mistakes helps you improve the more you see failure or broken code as an opportunity to learn the quicker you'll master programming i remember the first python script i ever wrote it took a lot of refactoring debugging and testing to get it to work i relied on a lot of my teammates for help and mentorship and wound up spending more time on stack overflow than actually writing the code thankfully you don't have to reinvent the wheel there's almost always someone on the internet who's tried to do what you're doing and can help point you in the right direction when you're stuck sometimes it takes a village it's really important to keep in mind that even experienced programmers may need to ask a colleague a question from time to time or look something up on the internet whether you're a programming novice or have some experience in software development remember the best programmers overcome challenges by seeking help or using other resources once you've completed this program you'll be well on your way to confidently programming in basic python there's lots of information online that will help you continue to develop your programming skills for example there are lots of online courses for specific programming languages you'll find answers to your python coding questions in the official python documentation you can use sites like stack overflow to discuss and share with other developers and you can ask questions in our discussion forums you can even subscribe to some of the python mailing lists to keep in the know on the latest updates to the language you're opening the door to the whole world of programming and it's super exciting to be joining the development community the most important thing to remember is that you're never alone any questions you may have any time in your career there are resources out there to help you find the answers you need [Music] although we picked python for this course it's important to note that it's just one of the many coding languages out there think of a given programming language as just one of the many powerful tools in your it toolbox each language has its unique set of pros and cons some run faster than others some are better suited for enterprise applications others are particularly good at crunching numbers there are platform specific scripting languages like powershell which is used on windows and bash which is used on linux both are widely used by system administrators on those platforms there are also general purpose scripting languages similar to python like perl or ruby which are also widely used for scripting and automation javascript which was originally developed as a client-side scripting language for the web is increasingly used as server side for a broader set of tasks and the list doesn't stop there there's a vast array of traditional languages to explore like c c plus plus java or go as you progress in your career in it you'll probably encounter a number of different languages and learn when to use each of them but let's not get ahead of ourselves first we have python to get our heads around a nice feature of learning the basics of programming in one language is that you can generally apply the same concepts you learn to other languages this means that once you're familiar with python you'll find it easier to pick up new coding languages as you'll spot and understand similarities and differences between them after all every language needs to do some common things like create variables control the flow of a program read input and display output even if they do these tasks using different approaches as we called out earlier learning a programming language is somewhat similar to learning a foreign language you'll need to grasp the syntax and semantics for that language luckily for us once you know the fundamentals of programming learning another language is much easier than learning a second foreign language there are a lot more similarities between programming languages than differences to explore some of the similarities and differences between various scripting languages let's take a look at a simple program that prints the words hello world 10 times in three different languages python bash and powershell as you can see each language uses a different approach to printing hello world but look closer and you'll see similarities too each language must somehow put text onto the screen the command for python is print for bash its echo and for powershell it's right host also notice that each language has to count to 10 in some way while python does this by specifying range 10 bash uses a sequence notation to count from 1 to 10. powershell has the most complex syntax in this example but it also boils down to starting at 1 and counting up to 10. so as we've just seen there's a whole lot of programming languages out there but don't let that scare you in this course you will only need to focus on learning python once you can speak python you can go on to learn any other language you want [Music] now that you've got an idea of what python code looks like let's check out one of the most basic examples and dive deeper into what's going on get ready we're going to use a python interpreter to make our computer say hello to the world when we run this code either locally on our machine or on a web interpreter the words hello world appear on the screen just like magic actually it's not magic it's because print is a python function that writes what we tell it to on the screen like the statement hello world for example the print function is part of the basic python language whenever we use keywords or functions that are part of the language we're using the programming languages syntax to tell the computer what to do so what are functions and keywords functions are pieces of code that perform a unit of work we'll talk a lot more about functions later on and you'll even learn how to write your own keywords are reserved words that are used to construct instructions these words are the core part of the language and can only be used in specific ways some examples include if while and for we'll explain all of those and a bunch more later in the course as we called out the keywords and functions used in python are what makes up the syntax of the language once we understand how they work we can use them to construct more complex expressions that get the computer to do what we want it to do last off notice how hello world is written between double quotation marks wrapping text in quotation marks indicates that the text is considered a string which means it's text that will be manipulated by our script in programming any text that isn't inside quotation marks is considered part of the code now for a bit of trivia do you know why we greeted the whole world in our example well printing hello world has been the traditional way to start learning a programming language since way back in the 70s when it was used as the first example in a famous programming book called the c programming language that example looked like this in python the hello world example is just one line in c it's three lines in other languages it could be even more while learning to write hello world won't teach you the whole language it gives you a first impression of how functions are used and how a program written in that language looks alright now that we've written our first piece of python code i think you're ready for something a bit more challenging than hello world ready let's do it [Music] on the whole for a program to be useful it needs to get at least some information from the user with this data the program can take actions that are relevant to the user instead of generic actions like printing hello world data can be provided to a computer in a bunch of different ways for example on a website you might input data by entering text into text fields or clicking links if you're using a mobile application maybe you'll click on buttons or select preferences from a drop down menu in a command line program you might provide additional data by passing strings as parameters to the program or you could have the program ask you for data interactively all these various platforms programs and apps process data differently some might take the contents of a file as data to be processed others gather data from other sources and process it in the background remember our earlier example when we automated the process of identifying and removing duplicate emails there the data provided to the program was the list of emails which would usually be given in a file that lists the emails one per line whichever way your application gets the data it will need to come from somewhere for our first examples in this course we'll just have the data as its own line in our block of code this is limited but straightforward later in this course and in upcoming courses we'll introduce you to better ways of feeding data into your code for now though let's see this idea in action in a very simple example by having the name separate from the call to the print function we're making the line of code that calls the print function generic while still personalizing the greeting if we then wanted to say hello to a different person we only need to change the name but the call to the print function will remain the same pretty simple right next up we'll learn a few other easy things that you can get python to do for you [Music] there's a ton of things that you could do with python and you'll learn many of them in this course but before we dive into complex subjects let's have some fun with another simple task that you could do with python we're going to make python our calculator and let's start with something easy so 4 plus 5 is 9 9 times 7 is 63 minus 1 divided by 4 is minus 0.25 easy repeating or periodic numbers are printed in a longer format let's try 1 divided by 3. in math theory when 1 is divided by 3 the digit 3 repeats forever after the decimal point of course it's hard to display something that repeats forever so instead we have a representation showing lots of decimal places not too hard right let's give the computer something a bit trickier let's say we want to divide 2050 by 5 then subtract 32 and then divide the result by 9. to do this we'll need to use parentheses just as we do in typical math problems you can also use python to get squares cubes or any power of n of a number for example let's say we want to find out what 2 to the power of 10 is to get python to give us the answer we use the double star operator if you're starting to worry that this is becoming an algebra course relax we're not going to do anything more complex than what we've just seen and if you're thinking why would i use python instead of just a normal calculator that's a valid question experimenting in this way you get familiar with the language's math capabilities and in it jobs there are many tasks that require you to use math calculations you might need to count how many times a certain word appears in a text or work out the average time it takes for an operation to complete or how much you have to compress an image to fit in certain size constraints whatever you need to calculate writing a script can help you do it faster and with more accuracy so you need to know what mathematical operations are available to you python actually has a lot more advanced numeric capabilities that are used for data analysis statistics machine learning and other scientific applications we won't get into these in this course but if you want to learn more about them on your own there's a wealth of online resources available congratulations on finishing this lesson in the google it automation with python certificate access the full experience including job search help and get the official certificate by clicking the icon or the link in the description watch the next lesson in the course by clicking here and subscribe to our channel for more lessons from upcoming google career certificates