DAY 1: Working with Variables
Printing, Commenting, Debugging, String Manipulation, and Variables
Last updated
Was this helpful?
Printing, Commenting, Debugging, String Manipulation, and Variables
Last updated
Was this helpful?
When we use the print( ) function, we add text inside the parentheses and this is known as a "string", think of a string as pearls on a necklace. Each pearl is a 'character' and so each character is joined with another character to form a "string"
If we wanted to print things on separate lines we would theoretically have to write the print statement 3 times like this:
print("Hello World")
print("Hello World")
print("Hello World")
Hello World
Hello World
Hello World
What we could do, is use one line of code with the "new line" function depicted as \n
print("Hello World\nHello World\nHello World")
Notice that the \n = new line and it is written as placed at the end of the word for the new line to start
Hello World
Hello World
Hello World
This is using the plus '+' sign to join strings together for example
print("Hello " + "Wallis, " + "How are you?")
Hello Wallis, How are you?
Dont forget the space between the end of the word and the end quotation. IF we dont have the space betwen them, concatenation will join all the words together to get:
HelloWallis,How are you
By adding spaces we can then get the text as normal. There is another way of concatenating strings together and that is using another '+' sign with double quotes
print("Hello" + " " + "Wallis," + " " + "How are you?")
Hello Wallis, How are you?
We use the 'input( )'
function if we want to enter data into Python. If we use the print( )
function and ask "What is your name? " - there is no way we can enter data to answer the question.
Here we will use the "input( )" command
For example"
input("What is your name?")
This code will ask the user exactly as asked and will now wait for some data input.
We type in Wallis
What the input code does is that the entire bit of code input(What is you name?")
gets replaced by my name Wallis
So lets see this in action: Lets say we issue the following command:
print(" Hello " + input("What is your name?"
))
The actual steps it takes is it replaces input("What is your name?")
with Wallis
print("Hello " + "Wallis")
------- it takes the inputWallis
and converted the input into a string "Wallis"
.
So we get:-
Hello Wallis
Its advisable as you code to write comments alongside your code for 2 main reasons:
When you have to revisit your code in the future, you can read your comments to understand what you did and,
When others use your code, they too will be able to understand what you have (or trying to) achieve
We can comment your thoughts. ideas etc by using the hash button ( # ) before the piece of text or code. Python ignores all hashed comments. We can add # tags on multiple lines by highlighting the text to be hash-tagged:
On a Windows and Linux machine (CTRL + /)
From our input exercise we checked the length (number of characters) in any given name and printed those to the screen. If we wanted to hold onto the output of the printed statement we could assign those number of characters to a "name" we could then refer to that name later on should we need to. We will remove the "print" statement and add a name in front followed by an equal sign (this equals sign does not mean that it matches that statement, but rather that a ' name' has been allocated to that output.
So for example:
print(" Hello " + input("What is your name?")) ------------- we will remove the print statement
name = input("What is your name?")
print(" Hello " + name))
Write a program that switches the values stored in the variables a and b. Example Input
a: 3 b: 5
Example Output:
a = 5 b = 3
Naming variables - basically you can call them anything you want however there are some rules and good practise techniques:
Never use the characters 'O' (uppercase letter oh), 'I' (uppercase letter eye) or 'l' (lowercase letter el), as single character variable names. (Think OIl (oil)
Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters (A-Z
, a-z
), digits (0-9
), and the underscore character (_
). An additional restriction is that, although a variable name can contain digits, the first character of a variable name cannot be a digit.
The most commonly used methods of constructing a multi-word variable name are the last three examples:
Camel Case: Second and subsequent words are capitalized, to make word boundaries easier to see. (Presumably, it struck someone at some point that the capital letters strewn throughout the variable name vaguely resemble camel humps.)
Example: numberOfCollegeGraduates
Pascal Case: Identical to Camel Case, except the first word is also capitalized.
Example: NumberOfCollegeGraduates
Snake Case: Words are separated by underscores.
Example: number_of_college_graduates
Programmers debate hotly, with surprising fervour, which of these is preferable. Decent arguments can be made for all of them. Use whichever of the three is most visually appealing to you. Pick one and use it consistently.
You will see later that variables aren’t the only things that can be given names. You can also name functions, classes, modules, and so on. The rules that apply to variable names also apply to identifiers, the more general term for names given to program objects.
Snake Case should be used for functions and variable names.
Pascal Case should be used for class names. (PEP 8 refers to this as the “CapWords” convention.)
There is one more restriction on identifier names. The Python language reserves a small set of keywords that designate special language functionality. No object can have the same name as a reserved word.
In Python 3.6, there are 33 reserved keywords:
Download if you ever get stuck and paste in your code and you can run the debug action and step into your code 1 line at a time to see how the code is being executed
Exercise 3: Inputs
The , also known as PEP 8, contains that list suggested standards for names of different object types. PEP 8 includes the following recommendations: