본문 바로가기
Coding : Python

Python_Day 1

by Jackykim 2023. 11. 10.

Printing, commenting, debugging, string Manipulation and variables


Printing to the console
We use the double quotation marks to indicate that anything in between those quotation marks are not code and just texts to print. These are called strings in programing. The double quotation marks shows the beginning and the end of the string of characters.

Print(“Hello world!”) however if you don’t close the double quotation mark you get a syntax error such as Print(“Hello World!)
When using pycharm and you use the command Print(“Hello World!) The first and last bracket colors will be different therefore you have to match the colors to prevent errors.

Print Function code test
Code should print all three line below
Day 1 - Python Print Function

The function is declared like this:

print('what to print')


To print all lines the print function should be in front of all lines as presented below
print("Day 1 - Python Print Function")

print("The function is declared like this:")

print("print('what to print')")

String manipulation and code intelligence
A method to print multiple strings with one print function (new line)
Print(“Hello World! \nHello World!”) would print hello world twice in two different lines such as below
Hello World!
Hello World!

To use the n with the print function code test above the code should look like this :
print("Day 1 - Python Print Function\n The function is declared like this: \n print('What to print'")

You can combine strings together into one with + this is called string concatenation.
Print(“Hello” + “name”) = Helloname however since there is no space function the two strings are connected. There are three ways to do this:
1. Spacing the end quotation on “Hello ”
2. Spacing the first quotation on “ name”
3. Using additional + and double quotation such as (“Hello” + “ “ + “name”)

Warning:
If you put a space or tab in front of the code in this case a print function, it will cause an indentation error so its important to not have a space in front of a code.

Debugging exercise (fix the errors below)
1. print(Day 1 - String Manipulation")

2. print("String Concatenation is done with the "+" sign.")

3.  print('e.g. print("Hello " + "world")')

4. print(("New lines can be created with a backslash and n.")

Fixed version:
1. print("Day 1 - String Manipulation")

2. print("String Concatenation is done with the " '"+"' " sign.")
or print(‘String Concatenation is done with the "+" sign.’)

3. print('e.g. print("Hello " + "world")')

4. print("New lines can be created with a backslash and n.")

Solution:
1. Missing Double quotation in front of Day 1
2. Use ‘ ‘ to include "+" into the code or alternate using single quote and double quote as a single code rather than two
3. There is an indentation in front of the code and getting rid of it fixes the code
4. There is an extra front bracket and getting rid of it fixes the code

The input function
When writing a input function the code is usually input(prompt) and once you run the code e.g
input(“What is your name?”) the cursor would not go to the next line until you put an input. Once you put an input that answer/input will become part of the prompt. For example if you input Python the code will become Python and you can string it together with print as below.
print(“Hello “ + input(“what is your name?”) -> Hello Python

Good practice to use # as a comment as the program will not treat it as a code or ctrl + /
# input() will get use input in console
# print() will print the word “Hello” and the user input

Input function exercise
If the num1 is 2 and num2 is 3 show the multiply without using print(“6”)
num1 = int(input())
num2 = int(input())
print(num1 * num2) -> this way it will always show num1 * num2 no matter the input

Input function exercise 2
Write a program that calculates and outputs the number of characters in any name.
The len function allows to count the number of characters in the string.
# Provide any name in the input pane below.

# That value can be accessed using the input() function.

# Don't put anything inside the input() function!
# answer -> print(len(input()))

Python Variables
You can setup a variable infront of the code that is attached to the value/input can be interacted later on. The variable can be changed later on.
E.g you can setup “name” as a variable in name = input(“What is your name?”) this will allow the input to become “name” therefore if you have print(name) in another line it will print the input.

 

For example when saving a phone number without a name we would not know who the phone number belongs to, however by putting a name or input it will help us identify the number more easily and that the name is similar to a variable in Python. In future when we need to refer to the data we can refer to it by using the name (variable). By using this knowledge we can make the code more concise and simpler.
e.g Instead of print(len(input(“What is your name?”))) you can use variables as below:
name = input(“what is your name?”)
length = len(name)
print(length)

Variables function exercise
Write a program that switches the values stored in the variables a and b.
# There are two variables, a and b from input

a = input()

b = input()

# 🚨 Don't change the code above ☝️

####################################

# Write your code below this line 👇

temp = a

a = b

b = temp


or

# Create a third variable to help switch the values
C = A

A = B

B = C

 

# 🚨 Don't change the code below 👇

print("a: " + a)

print("b: " + b)

Variable Naming
You can change the variable names however it has to be easy to understand and easy to remember when you come back to the code after a long period of time. Variable name has to be one word using a _ such as user_name. If you want to use numbers in the variables, it has to come after the variable such as Length1. 1Length would cause syntax errors. In addition the spelling has to be identical.
The example below is a case of bad variable naming since n or l could mean anything.
n = input(“what is your name?”)
l = len()
print(l)

Project 1
Creating a band name Generator using city name and pet name.

 

1. Start by using the print function to show the message

print(“Welcome to the Band Name Generator.”)

2. Asking the question regarding the city

Street = input(“What’s the name of the city you grew up in?\n”)

3. Asking the question regarding your pet after the city
pet = input(“What’s your pet’s name?\n”)

4. Using the print function to show the answer
print(“Your band name could be “ + street + “ “ + pet)


Note : \ is backward slash 

'Coding : Python' 카테고리의 다른 글

Python_Day 6  (0) 2023.11.30
Python_Day 5  (1) 2023.11.23
Python_Day 4  (0) 2023.11.17
Python_Day 3  (0) 2023.11.15
Python_Day 2  (0) 2023.11.10