Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Dcs Python 01 Note

Uploaded by

Hasnain Raza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views

Dcs Python 01 Note

Uploaded by

Hasnain Raza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
Introduction = So far, we've discussed how to build simple web pages using HTML and CSS, and how to use Git and GitHub in order to keep track of changes to our code and collaborate with others = Today, we'll dive into Python, one of the two main programming languages we'll use throughout this course. Python ® python’ = Python is a very powerful and widely-used language that will allow us to quickly build fairly complicated web applications. In this course, we'll be using Python 3, although Python 2 is still in use in some places. When looking at outside resources, be careful to make sure they're using the same version. = Let's start where we start with many programming languages: Hello, world. This program, written in Python, would Look like this: print("Hello, world!) = To break down what's going on in that line, there is a print function built in to the python language, that takes an argument in parentheses, and displays that argument on fe the command ® To actually write and run this program on your computers, you'll first type this line into your text editor of choice, and then save the file as something. py . Next, you'll head over to your terminal, navigate to the directory containing your file, and type python sonething. py .In the case of the above program, the words "Hello, world!” will then be displayed in the terminal. = Depending on how your computer is set up, you may have to type python3 instead of python before the file name, and you may even have to download Python https://www.python.org/downloads/) if you haven't already. After installing Python, we recommend that you also download Pip (https://pip.pypa.io/en/stable/installin: you'll need that later in the course. = When you type python file.py in your terminal, a program called an interpreter, which you downloaded together with Python, reads through your file line by line, and executes each line of the code. This is different than languages like C or Java, which need to be compiled into machine code before they can be run. Variables Akey part of any programming language is the ability to create and manipulate variables. In order to assign a value to a variable in Python, the syntax looks like this = 28 21.5 = "Hello!" True None Each of these lines is taking the value to the right of the =, and storing it in the variable name to the left. Unlike in some other programming languages, Python variable types are inferred, meaning that while each variable does have a type, we do not have to explicitly state which type it is when we create the variable, Some of the most common variable types are = int: An integer = float: A decimal number = str: A string, or sequence of characters = bool: Avalue that is either True or False = NoneType: A special value (None ) indicating the absence of a value. Now, we'll work on writing a more interesting program that can take input from the user and say hello to that user. To do this, we'll use another built in function called input which displays a prompt to the user, and returns whatever the user provides as input. For example, we can write the following ina file called nane.py name = input("Name: ") print("Hello, " + name) When run on the terminal, this is what the program looks like: (base) cleggett@Connors-MacBook-Pro web_notes_files % python hello.py Name: Connor Pon CRMCcs (base) cleggett@Connors-MacBook-Pro web_notes_files % A couple of things to point out here: & Inthe first line, instead of assigning the variable name to an explicit value, we're assigning it to whatever the input. function returns. ® Inthe second line, we're using the + operator to combine, or concatenate, two strings. In python, the + operator can be used to add numbers or concatenate strings and lists. Formatting Strings = While we can use the + operator to combine strings as we did above, in the latest versions of python, there are even easier ways to work with strings, known as formatted strings (https://realpython.com/python-f-strings/), or f-strings for short. = To indicate that we're using formatted strings, we simply add an £ before the quotation marks, For example, instead of using “Hello, “ + nane as we did above, we could write "Hello, {nane)" for the same result. We can even plug a function into this string if we want, and turn our program above into the single line: print(f"Hello, {input("Name: ")}") Conditions = Just like in other programming languages, Python gives us the ability to run different segments of code based on different conditions (https/realpython.com/python- conditional-statements/). For example, in the program below, we'll change our output depending on the number a user types in: num = input "Number: if num > @ print(“Number is positive” elif num < @: print("Number is negative") else: print("Number is @") » ® Getting into how the above program works, conditionals in python contain a keyword (if, elif ,or else) and then (except in the else case) a boolean expression, or an expression that evaluates to either True or False. Then, all of the code we want to run if a certain expression is true is indented directly below the statement. Indentation is required as part of the Python syntax. = However, when we run this program, we run into an exception (https://docs.python.org/3/tutorial/errors.html) that looks like this: Dt ad eee Ce ee File "cond.py", line 2, in eee TypeError: '>' eee ee Ce a (base) cleggett@Connors-MacBook-Pro web_notes_files % = An exception is what happens when an error occurs while we're running our python code, and over time you'll get better and better at interpreting these errors, which is a very valuable skill to have, = Let's Look a bit more closely at this specific exception: If we look at the bottom, we'll see that we ran into a TypeError , which generally means Python expected a certain variable to be of one type, but found it to be of another type. In this case, the exception tells us that we cannot use the > symbol to compare a str and int ,and then above we can see that this comparison occurs in line 2. = In this case, its obvious that @ is an integer, so it must be the case that our nun variable is a string, This is happening because it turns out that the input function always returns a string, and we have to specify that it should be turned into (or cast into) an integer using the int function. This means our first line would now Look like: num = int(input("Number: ")) = Now, the program will work just as we intended! Sequences One of the most powerful parts of the Python language is its ability to work with sequences of data in addition to individual variables. There are several types of sequences that are similar in some ways, but different in others. When explaining those differences, we'll use the terms mutable/immutable and ordered/unordered, Mutable means that once a sequence has been defined, we can change individual elements of that sequence, and ordered means that the order of the objects matters. Strings Ordered: Yes Mutable: No We've already looked at strings a little bit, but instead of just variables, we can think of a string as a sequence of characters. Thit means we can access individual elements within the string!

You might also like