0% found this document useful (0 votes)
30 views

Python Programming and Programming Concept

The document provides information about variables, data types, and basic input/output functions in Python. It includes examples of defining integer, float, string, and boolean variables; taking user input using the input() function and converting the data type; and displaying output to the console using the print() function. The document also discusses dynamically typed variables in Python and how a variable's type is inferred from its assigned value.

Uploaded by

victorwu.uk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Python Programming and Programming Concept

The document provides information about variables, data types, and basic input/output functions in Python. It includes examples of defining integer, float, string, and boolean variables; taking user input using the input() function and converting the data type; and displaying output to the console using the print() function. The document also discusses dynamically typed variables in Python and how a variable's type is inferred from its assigned value.

Uploaded by

victorwu.uk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer Science in Year 8

Python programming and programming concept


Python Tutorial https://www.w3schools.com/python/default.asp

Topic : variable, assign a value to a variable


https://www.w3schools.com/python/python_variables.asp
In Python, a variable is a named storage location used to store data. It acts as a container that holds
a value, which can be of different types such as numbers, strings, lists, or even more complex
objects. Variables allow you to manipulate and reference data throughout your program.

In Python, you can define a variable by assigning a value to it using the equals sign (=). Here's a basic
syntax for declaring a variable:

variable_name = value

For example, let's create a variable named `message` and assign it the value "Hello, world!":

message = "Hello, world!"

In this case, the variable `message` is assigned the string value "Hello, world!".

Python is a dynamically-typed language, which means you don't need to explicitly declare the type
of a variable. The type of a variable is inferred from the value assigned to it. For example, if you
assign an integer value to a variable, it becomes an integer variable, and if you assign a string value,
it becomes a string variable.
Here's an example demonstrating variables of different types:

# Integer variable
age = 25

# Float variable
pi = 3.14159

# String variable
name = "John Doe"

# Boolean variable
is_valid = True

In this example, `age` is an integer variable with a value of 25, `pi` is a float variable with a value of
3.14159, `name` is a string variable with a value of "John Doe", and `is_valid` is a boolean variable
with a value of True.
You can also update the value of a variable by assigning it a new value:

count = 10
count = count + 1 # Updating the value of count
print(count) # Output: 11

In this case, the value of `count` is initially 10. Then, it is updated by adding 1 to it, resulting in a
new value of 11.

Variables in Python are flexible, and their values can be changed and reassigned throughout the
program as needed.
Topic : using input()
https://www.w3schools.com/python/ref_func_input.asp
To get keyboard input from the user using the `input()` function in Python, you can follow this
syntax:

variable = input(prompt)

Here's an example that prompts the user to enter their name and stores it in the `name` variable:

name = input("Enter your name: ")

When this code is executed, it displays the message "Enter your name: " on the console and waits
for the user to input their name. Once the user enters their name and presses Enter, the input is
captured and assigned to the `name` variable.

You can then use the value stored in the variable for further processing or display it as needed. For
example, you can print a personalized greeting using the user's input:

print("Hello, " + name + "! Nice to meet you.")

In this case, the user's name is concatenated with the greeting message and printed to the console.

It's important to note that the `input()` function always returns a string, regardless of the user's
input. If you need to convert the input to a different data type, such as an integer or float, you can
use appropriate type casting or conversion functions like `int()` or `float()`.
Here's an example that prompts the user for their age and converts the input to an integer:

age = int(input("Enter your age: "))

In this case, the `input()` function captures the user's input as a string, and then `int()` is used to
convert that string to an integer.

Remember to handle user input with caution and validate it as needed to ensure it meets your
program's requirements.
Topic : using print()
https://www.w3schools.com/python/ref_func_print.asp
In Python, you can display text or output to the screen using the `print()` function. The `print()`
function takes one or more arguments and displays them as output.

Here's a basic example of using the `print()` function to display text:

print("Hello, world!")

In this case, the `print()` function outputs the text "Hello, world!" to the screen.

You can also pass multiple arguments to the `print()` function, separating them with commas. Each
argument will be displayed with a space between them:

name = "John"
age = 25
print("My name is", name, "and I am", age, "years old.")

In this example, the output will be "My name is John and I am 25 years old."

The `print()` function automatically adds a newline character (`\n`) at the end of each output. If you
want to change this behavior and prevent the newline character from being added, you can use the
`end` parameter:

print("Hello", end="")
print(", world!")

In this case, the output will be "Hello, world!" without a newline between them.

You can also use escape characters to format the output. For example:

print("This is a\ttab.")
print("This is a\nnew line.")
print("This is a backslash: \\")

In this example, the output will be:


Output
This is a tab.
This is a
new line.
This is a backslash: \

The `print()` function supports various formatting options, such as specifying the number of decimal
places for floating-point numbers or formatting strings using placeholders. However, those options
go beyond the basic usage of `print()`. If you need more advanced formatting, you can explore the
`format()` method or formatted string literals (f-strings).

Remember, the `print()` function is a versatile tool for displaying text and output during program
execution, allowing you to provide information or interact with the user.
Topic : a sequence of instructions

Certainly! Here are the examples again with the expected output:

1. Calculating the Area of a Rectangle:


```python
# Input the length and width of the rectangle
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# Calculate the area of the rectangle


area = length * width

# Display the result


print("The area of the rectangle is:", area)
```

Example output:
```
Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
The area of the rectangle is: 15.0
```

2. Converting Celsius to Fahrenheit:


```python
# Input the temperature in Celsius
celsius = float(input("Enter the temperature in Celsius: "))

# Convert Celsius to Fahrenheit


fahrenheit = (celsius * 9/5) + 32

# Display the result


print("The temperature in Fahrenheit is:", fahrenheit)
```

Example output:
```
Enter the temperature in Celsius: 25
The temperature in Fahrenheit is: 77.0
```

3. Checking if a Number is Even or Odd:


```python
# Input a number
number = int(input("Enter a number: "))

# Check if the number is even or odd


if number % 2 == 0:
print(number, "is even.")
else:
print(number, "is odd.")
```

Example output:
```
Enter a number: 10
10 is even.
```

4. Finding the Maximum of Three Numbers:


```python
# Input three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Find the maximum of the three numbers


maximum = max(num1, num2, num3)

# Display the result


print("The maximum number is:", maximum)
```

Example output:
```
Enter the first number: 15
Enter the second number: 8
Enter the third number: 21
The maximum number is: 21.0
```

5. Generating a Multiplication Table:


```python
# Input a number
number = int(input("Enter a number: "))

# Generate and display the multiplication table


for i in range(1, 11):
product = number * i
print(number, "x", i, "=", product)
```

Example output:
```
Enter a number: 7
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
```

These examples include the expected output for each program, helping students understand the
results they should see when running the code.
Topic : use + can also be used to CONCATENATE (join) two strings (text) .

Certainly! Here are five examples of using the `+` operator to concatenate or join two strings
together in Python:

1. Concatenating Two Strings:


```python
# Concatenating two strings
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)
```

Example output:
```
Hello World
```

2. Concatenating Strings and Variables:


```python
# Concatenating a string and a variable
name = "John"
age = 25
result = "My name is " + name + " and I am " + str(age) + " years old."
print(result)
```

Example output:
```
My name is John and I am 25 years old.
```

3. Concatenating Strings with User Input:


```python
# Concatenating a string with user input
name = input("Enter your name: ")
result = "Hello, " + name + "! Welcome."
print(result)
```

Example output:
```
Enter your name: Alice
Hello, Alice! Welcome.
```

4. Concatenating Strings in a Loop:


```python
# Concatenating strings in a loop
fruits = ["apple", "banana", "orange"]
result = ""
for fruit in fruits:
result += fruit + " "
print(result)
```

Example output:
```
apple banana orange
```

5. Concatenating Strings with Leading Zeros:


```python
# Concatenating strings with leading zeros
number = 7
padded_number = "0" + str(number)
print(padded_number)
```

Example output:
```
07
```
These examples showcase different scenarios where the `+` operator is used to concatenate strings.
It demonstrates how strings can be combined with other strings, variables, user input, or within
loops. Additionally, it highlights the flexibility of the `+` operator in concatenating strings with
leading zeros.

You might also like