0% found this document useful (0 votes)
7 views2 pages

Python Basics: Operators and Functions

The document provides an introduction to Python programming, covering arithmetic, comparison, logical, bitwise, and assignment operators. It also includes functions for input and output, along with practical programming exercises to apply these concepts. The exercises range from basic calculations to more complex tasks involving mathematical formulas and data manipulation.

Uploaded by

chahid nizar
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)
7 views2 pages

Python Basics: Operators and Functions

The document provides an introduction to Python programming, covering arithmetic, comparison, logical, bitwise, and assignment operators. It also includes functions for input and output, along with practical programming exercises to apply these concepts. The exercises range from basic calculations to more complex tasks involving mathematical formulas and data manipulation.

Uploaded by

chahid nizar
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

TP1: Introduction to Python

Open the Python REPL SHELL (stands for Read-Eval-Print Loop), or inside Spyder/Pyzo editors.
Make the calculations below.

Partie 1 (Arithmetics)
Arithmetic operators (+, -, *, / (real division), % (modulo), //(quotient of Euclidean
division), ** )
5 + 17; 22 - 30; 4 * 13; 3/4; 35 % 3; 3//4; 3**.5
(1 + √7)÷(3 - √7) (with/without parentheses)
6 * 3 + 4 ** 2 // 5 - 8 (with/without parentheses)
Order of operations:
1. Parentheses
2. Exponentiation
3. Division
4. Addition

Comparison operators (<,>, ==, !=, >=, <=)< /h3>

The result of comparison operation is a boolean value (True=1 / False=0)


3 > 5; 5 < 7; 3==4; 3!=4; 6==6; 6<=5

Logical operators (and, or, not)


(13 > 5) and (2==3)
(13 > 5) or (2==3)
not (13 > 5)
3<5<7
1>4<2
1 < 4> 2

Bitwise operators ( & , | , ~ , ^ , << ,>> )

3 & 4 => 0 (bitwise and)


3 | 4 => 7 (bitwise or)
3 ^ 4 => 7; 3 ^ 5 => 6 (bitwise xor)
1 << 3=> 2**3 ; 3 << 1=> 6 ; 3 << 2=> 12 (left shift operator)
3 >> 1 => 1 ; 3 >> 2 => 0 (right shift operator)

Assignment operators ( = , += , -= , *= , /= , %= , //= , **= , &= , |= , ^= , >>= , <<= )

Arithmetic assignment operators

a=1
a += 2 # 3
a -= 1 # 2
a *= 3 # 6
a /= 2 # 3
a %= 2 # 1
a //= 0.3 # 3
a **= 2 # 9

Bitwise assignment operators

b = 13
b &= 12 # 12
b |= 0 # 12
b ^= 3 # 15
b <<= 5 # 480
b >>= 5 # 15

Partie 2 (Fonction Usuelle)


function type

s = "12"; type(s);
i = 12; type(i);
f = 3.14e7; type(f);
c = 1+j2; type(c);

function print

a = 12; f = 3.14; s = "world";


print("hello", s);
print(a, f, 1, 0.5, True, "hello", s, 1+2j);

function input

Simple input
name = input("What is your name:");
print("Hello, ", name);
Numerical input
age = input("How old are you:");
age = int(age);
print("You are ", age, " years old");
math = input("what is your math's note:");
math = float(math);
print("You've got ", math, " in math exam");

Partie 3 (Premiers programmes en Python)


1. Given an integer a, print the value of: a + a2 + a3
2. find the value of : 3*x + 5*(√x - y2) in the following cases: (x,y) = (3, 5), (-1, -1), (-1, 2)
3. Given 2 integers a and b, find the quotient (q) and the remainder (r).
4. Given the temperature in Celsius, convert it to Fahrenheit and vice-versa using the formula: C = (F -
32) * 5/9.
5. Given 2 integers a and b, find a way to change their values: example initially a=3, b=5, so finally we
have a==5 and b==3.
6. Given 2 integers a and b, find the min(a,b) / max(a,b).
7. Given the coordinates of the A, B, and C of a triangle, find its area using heron formula.
8. Given the time (h, m, s), find the time after x seconds.
9. Given 3 cards having letters a,b and c placed in a row, is it possible to sort it in at most one swap of 2
of them. the user will input the order like : "bca" and your program must print "yes" or "no".

You might also like