CS G10 Semester 2 Mid-Term
CS G10 Semester 2 Mid-Term
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))
a=4
A = "Sally"
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
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
# Output
print(type(name))
Output:
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)
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:
These conditions can be used in several ways, most commonly in "if statements" and loops.
If statement:
a = 33
b = 200
if b > 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.
a = 33
b = 200
if b > a:
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:
elif a == b:
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:
elif a == b:
else:
Short Hand If
If you have only one statement to execute, you can put it on the same line as the if statement.
a=2
b = 330
a = 330
b = 330
And
The and keyword is a logical operator, and is used to combine conditional statements:
a = 200
b = 33
c = 500
Or
The or keyword is a logical operator, and is used to combine conditional statements:
a = 200
b = 33
c = 500
if a > b or a > c:
a = 33
b = 200
if not a > 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:
else:
a = 33
b = 200
if b > a:
pass