PYTHON
  • PYTHON v3
  • Python IDE Setup
  • Python Programming
    • Python for ABS
      • Resources
      • Ch1 - Getting Started
      • Ch2 - Types, Variables and Simple I/O
  • Python For Network Engineers-Kirk Beyers
    • Resources
    • Python Fundamentals
  • Python Inststitute (PCAP)
    • Resources
    • Course Introduction
    • Python Essentials (Part 1)
      • Module 1
        • Fundamentals
        • What is Python
        • Starting your work with Python
      • Module 2
        • The Print Function
          • Section Summary
        • Literals
          • Section Summary
        • Operations- Data Manipulation Tools
          • Section Summary
        • Variables
          • Leaving Comments in Code
          • The input () Function
  • 100 Days Of Code
    • Resources
    • What You Have To Do !!
    • 100 DAY's
      • DAY 1: Working with Variables
      • Day 2: Data Types & String Manipulation
      • Day 3: Control Flow and Operators
      • Day 4: Randomisation and Lists
      • Day 5: For Loops, Range & Code Blocks
      • Day 6: Python Functions and Karel
      • Day 7: Hangman
      • SUMMARY
  • {Pirple}
    • Resources
Powered by GitBook
On this page
  • Printing to the Console
  • Exercise 1: (repl.it link)
  • String Manipulation and Code Intelligence
  • Concatenate Strings
  • Exercise 2: Debugging Practise (repl.it link)
  • The Python 'input ( )' Function
  • Commenting
  • Python Variables
  • Exercise: Switching Variables
  • Variable Naming - PEP8
  • Function and Variable Names
  • Reserved Words (Keywords)
  • Daily Project: Band Name Generator

Was this helpful?

  1. 100 Days Of Code
  2. 100 DAY's

DAY 1: Working with Variables

Printing, Commenting, Debugging, String Manipulation, and Variables

Previous100 DAY'sNextDay 2: Data Types & String Manipulation

Last updated 3 months ago

Was this helpful?

Printing to the Console

Exercise 1:

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"

String Manipulation and Code Intelligence

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

Concatenate Strings

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?

#Write your code below this line 👇
print("Day 1 - Python Print Function")
print("The function is declared like this:")
print("print('what to print')")


print("Hello World")
print("Hello World")
print("Hello World")


print("Hello World\nHello World\nHello World")

print("Hello" + "Wallis," + "How are you?")

print("Hello" + " " + "Wallis," + " " + "How are you?")

The Python 'input ( )' Function

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

Commenting

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 + /)

Python Variables

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))

Exercise: Switching Variables

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.)

Reserved Words (Keywords)

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:

Daily Project: Band Name Generator

welcome = len(input("Welcome to the World Famous 'Band Name Generator'.\n"))
city = input("What's the name of the City you grew-up in? \n")
pet = input("What's your Pets name?\n")
band_name = print("Your band name could be "+ city + " " + pet)

Exercise 2: Debugging Practise

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

Variable Naming -

The , also known as PEP 8, contains that list suggested standards for names of different object types. PEP 8 includes the following recommendations:

(repl.it link)
Thonny
(repl.it)
PEP8
Function and Variable Names
Style Guide for Python Code
Naming Conventions
(repl.it link)