Computer science
Revision for mid-term
Grade 10
Second semester
Done by:
Ahmed Essam
Computer science
Computer languages
• To communicate with computers, we need some languages.
There are mainly three different languages with the help of which we can develop computer programs.
And they are:
• Machine-Level language
• Assembly-Level Language and
• High-Level Language
Variables
Variables are containers for storing data values.
In a program, the data is stored in 2 ways – Either the data is already stored in the program, or the data
comes from the user. The data is stored in both ways. The program stores this data in the form of
Variables. Every variable has its own data type, a name, and a value assigned to it. The value of that
variable might change as time goes by, and hence the name Variable.
We will study python so there are some examples of the variable from it:
x=5 → integer
y = "John" → String
• You can get the data type of a variable with the type() function.
print(type(x))
print(type(y))
• Variable names are case-sensitive.
a=4
A = "Sally"
(A) and (a) aren’t the same variable.
➢ A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
1. A variable name must start with a letter or the underscore character
2. A variable name cannot start with a number
3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
4. Variable names are case-sensitive (age, Age, and AGE are three different variables)
5. A variable name cannot be any of the Python keywords.
Data types
In programming, a data type specifies which type of value a variable has and what type of mathematical,
relational, or logical operations can be applied to it without causing an error.
Text Type:
x = "Hello World"
Numeric Types:
integer x = 20
decimal y = 20.5
Boolean Type:
x = True
Input and output
Sometimes a developer might want to take user input at some point in the program. To do this Python
provides an input() function.
Syntax:
input('prompt')
where prompt is an optional string that is displayed on the string at the time of taking input.
Example 1: Python get user input with a message
# Taking input from the user
name = input("Enter your name: ")
# Output
print("Hello, " + name)
print(type(name))
Output:
Enter your name: GFG
Hello, GFG
<class 'str'>
Python Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
print(10 + 5)
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
➢ Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
➢ Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
➢ Python Comparison Operators
Comparison operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
➢ Python Logical Operators
Logical operators are used to combine conditional statements:
Operator Description Example
And Returns True if both statements are true x < 5 and x < 10
Or Returns True if one of the statements is true x < 5 or x < 4
Not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
➢ Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the
same object, with the same memory location:
Operator Description Example
Is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y
➢ Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
➢ Operator Description Example
in Returns True if a sequence with the specified value is x in y
present in the object
not in Returns True if a sequence with the specified value is x not in y
not present in the object
➢ Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operator Name Description Example
& AND Sets each bit to 1 if both bits are 1 x&y
| OR Sets each bit to 1 if one of two bits is 1 x|y
^ XOR Sets each bit to 1 if only one of two bits is 1 x^y
~ NOT Inverts all the bits ~x
<< Zero fill left shift Shift left by pushing zeros in from the right and x << 2
let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in x >> 2
from the left, and let the rightmost bits fall off
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
If statement:
a = 33
b = 200
if b > a:
print("b is greater than a")
In this example we use two variables, a and b, which are used as part of the if statement to test whether
b is greater than a.
As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater
than a".
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other
programming languages often use curly brackets for this purpose.
If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this
condition".
a = 33
b = 33
if b > a:
print("b is greater than
a")
elif a == b:
print("a and b are equal")
In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print
to screen that "a and b are equal".
Else
The else keyword catches anything which isn't caught by the preceding conditions.
In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so
we go to the else condition and print to screen that "a is greater than b".
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
You can also have an else without the elif:
Short Hand If
If you have only one statement to execute, you can put it on the same line as the if statement.
One line if statement:
if a > b: print("a is greater than b")
Short Hand If ... Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the same
line:
One line if else statement:
a=2
b = 330
print("A") if a > b else print("B")
This technique is known as Ternary Operators, or Conditional Expressions.
You can also have multiple else statements on the same line:
One line if else statement, with 3 conditions:
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
And
The and keyword is a logical operator, and is used to combine conditional statements:
Test if a is greater than b, AND if c is greater than a:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Or
The or keyword is a logical operator, and is used to combine conditional statements:
Test if a is greater than b, OR if a is greater than c:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Not
The not keyword is a logical operator, and is used to reverse the result of the conditional statement:
Test if a is NOT greater than b:
a = 33
b = 200
if not a > b:
print("a is NOT greater than b")
Nested If
You can have if statements inside if statements, this is called nested if statements.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
The pass Statement
if statements cannot be empty, but if you for some reason have an if statement with no content, put in
the pass statement to avoid getting an error.
a = 33
b = 200
if b > a:
pass