0% found this document useful (0 votes)
6 views15 pages

Python Lesson 1

Uploaded by

Diana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Python Lesson 1

Uploaded by

Diana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python

print("Welcome")

The print() instruction is the easiest way to send a message to the screen
Numbers don't require quotation marks.

Print (360)

Computer programs use variables to remember important information, like items in a shopping
cart, prices and discounts.
This is a variable called item. The information that you need to store is added on the right.

item = "bike"

A large amount of information out there consists of text. A piece of text data is called a string.
Strings in Python need to be surrounded by quotation marks.
In Python, both single ' and double " quotes can be used to define strings

item = ‘bike’
The code in computer programs is made of statements. Statements are the instructions for the
computer to follow. Real programs can contain thousands of statements.
Here there are 2 statements

book = "Harry Potter"

author = "J. K. Rowling"

Numerical values can be directly stored in variables.

Population= 2500
You can perform math operations with numbers.

print(7+3)

print(10-5)

print(5*3)

print(10/2)

You can make calculations using the values in variable

budget=20

print(budget+10)

Here the answer will be 30

price = 5

amount = 3

print(price * amount)

Answer 15

score = 7 + 8

print(score)
Answer 15

Updating the value of a variable is called reassigning a variable.

points = 35

points = 45

print(points)

This will show 45

We create the file with .py

Ctrl+? = comment

a = 16 # integer -> int() numeric


b = 16.5 # float -> decimal number
c = "uinm" # string -> str() text
d = "65" # this is a string

print(type(d)) # if we don't know whether it is string or numeric, we use this


A variable has a name and a value.
In JavaScript, variables are declared using Boolean, let, var, and const, while in Python you
simply write the name and assign a value.

There are 4 types of variables in Python:

1. Numeric variable (integer) – without decimals.


2. Float (decimal number) – with decimals.
3. String variable – written inside quotes " ".
4. Boolean (True, False) – for example:

g = True

h = False

Input – the variable value is entered by the user.

a = input("How old are you?") # this is a string input


b = int(input("Enter a number")) # this is an integer input

print(a)
print(b)

or

# Asking user for details


name = input("Enter your name: ") # string input
surname = input("Enter your surname: ") # string input
age = int(input("Enter your age: ")) # integer input
gender = input("Enter your gender: ") # string input
birthdate = input("Enter your birthdate (DD/MM/YYYY): ") # string input
phone = input("Enter your phone number: ") # string input

# Printing the collected data


print("\n--- User Information ---")
print("Name:", name)
print("Surname:", surname)
print("Age:", age)
print("Gender:", gender)
print("Birthdate:", birthdate)
print("Phone Number:", phone)

or
print("----- Sign Up Form -----")

username = input("Enter a username: ")


email = input("Enter your email: ")
password = input("Enter a password: ")
confirm_password = input("Confirm your password: ")

# Checking password match


if password != confirm_password:
print("Passwords do not match! Please try again.")
else:
full_name = input("Enter your full name: ")
age = int(input("Enter your age: "))
gender = input("Enter your gender (Male/Female/Other): ")
phone = input("Enter your phone number: ")
country = input("Enter your country: ")

print("\n--- Account Created Successfully! ---")


print("Username:", username)
print("Email:", email)
print("Full Name:", full_name)
print("Age:", age)
print("Gender:", gender)
print("Phone:", phone)
print("Country:", country)

Debugging
From the 2nd line the execution will stop

salary = 50000

role = Analyst

age -> 29

print(salary)

You can add comments to your Python code with the hash symbol #
Python is a case-sensitive language, meaning "A" and "a" are treated as different.

credit = 300

Credit = 280

CREDIT = 320

Coders use snake case to give descriptive names to variables with multiple words. The
underscore makes the variable name easier to read.

user_id

A variable name can contain numbers but cannot start with a number.
Input and Outputs
An input is any information that goes into a computer.

The press of a key and the click of a button are examples of inputs.

 input() waits for the user to type something and press Enter.

 Whatever the user types is stored in the variable user_entry.

user_entry = input()

An output is a way for the computer to communicate with the outside world. A message
displayed on the screen and the sound from a speaker are examples of outputs.

The print() instruction, which you already know, is the easiest way to get your computer
program to generate an output.
print(“Welcome”)

Data Types

Data comes in different shapes and forms. Computers treat different types of data in
different ways.
String - “Anna”
String is the data type for a piece of text. The quotation marks tell the computer that a
value needs to be stored as a string.
Computers store and process different types of numbers. Integers are whole numbers
without a decimal point. They can be positive, negative or zero. – e.g. -2
Float is the data type for numbers with decimal places, they can be positive or negative.
e.g.- 3.14

When you use the + addition operator with string values the two strings are joined
together.
This is known as concatenation.
a=”basket’’

b=”ball”

print(a+b)
This will display- basketball

Anything in quotation marks will be treated as a string, even numbers.


This is a string:
var = "360"

Data can come to you in the incorrect format. You can use the type() instruction to check the
data type stored in a variable.

balance = "780"

print(type(balance))

It will show string

the input() function in Python is designed to read everything the user types as text — it doesn’t try to
guess or convert it automatically. So even if you type a number like 123, Python treats it as the string
"123".
Math operations between integers and floats produce a float.

x=9

y = 3.0

print(x+y)

Result 12.0

Python automatically converts the result of / into a float, even if both numbers are integers — that’s
implicit conversion (done by Python without you asking).

print(12/6)

Comparison Operations

A comparison operation always results in either one of these two outcomes: Yes or No
Computers use binary code to represent information. By turning switches ON and OFF, we
change the information stored in a computer.
To simplify things, two numbers are used to represent the OFF/ON states of a switch- 0 and 1.
Comparison operations and Boolean values allow machines to make decisions –True or False.

Logical operations are needed for machines to evaluate complex scenarios.


Logical operations use Boolean values.
The "and" operation results in a True value only when all the inputs are True at the same time.

True and True

Result -True

A logical operation combines Boolean inputs to produce a Boolean output.

True and True = True


Every other combination = False

True and False


Result-False

The "or" logical operation results in a True value if at least one of the inputs is True.

True or False

Result -True
You can store boolean values in variables just like you do with other data types.
sleep=True

Control Flow

You already know sequencing. It means that the computer runs your code in order, from top to
bottom.
Selection specifies when to follow each path.

Smartwatches notify the wearer if their heart rate goes outside the normal range. This is
an example of selection.
Machines can complete complex tasks for us, but first they need to know how.

An algorithm is a set of step-by-step instructions to complete a task, placed in a certain


order.
Algorithms exist in our everyday lives.
The step-by-step instructions in a cooking recipe are an example of an algorithm.

You might also like