3 For Loop, Sttipaza42
3 For Loop, Sttipaza42
اإلعالم اآللي
GIVEN BY DR. HADJ AMEUR & DR. MANSOURI
YEAR 2023-2024
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
Comments in python
Comments in python are like little notes you can leave within your code to help
yourself or others understand what the code does. They don't affect how the
program runs; they are there for humans to read only. Comments are important
because they make your code more readable and help you remember what your
code is doing.
➔In python, you can create comments by using the # symbol. When python sees a
#, it ignores everything after it on the same line.
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
Comments in python
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
The break line character "\n"
In python the special character \n tells python to break a line (go to a new line)
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES
UNDERSTANDING LOOPS IN PYTHON
Loops are a fundamental concept in programming that allow you to repeat a set of
instructions multiple times. They are essential because they enable programmers to automate
repetitive tasks and iterate through data efficiently.
➔In programming, loops are like the "repeat" button for your code. They let you perform the
same actions over and over again.
In Python, there are two main types of loops: for loops and while loops. Each
type of loop has its specific use cases and is chosen based on the problem
you want to solve.
1- For Loops:
Description: For loops are used when you know in advance how many times
you want to repeat a block of code. They are often used for iterating over
sequences or collections like lists, tuples, strings, or when you want to execute a set
of statements a specific number of times.
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES
=> FOR LOOP
The syntax of a "for" loop in Python is as follows:
range(start, stop)
range(stop)
Exercises:
1. Write a python program that read an Integer x and print “hello” x times ?
2. Write a python program that read a string name and print each one of its
chars in separate lines ?
3. Write a python program that prints the first 5 odd numbers starting from 0?
4. Write a python program that reads two numbers x and y and prints all the
numbers in [x,y] and then in ]x,y[
5. Write a program that reads five real numbers and calculate their sum and
their average ?
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP
1. Write a python program that read an Integer x and print “hello” x times ?
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP
2. Write a python program that read a string name and print each one of its
chars in separate lines ?
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP
3. Write a python program that prints the first 5 odd numbers starting from 0?
Solution 1:
Solution 2:
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP
4. Write a python program that reads two numbers x and
y and prints all the numbers in [x,y] and then in ]x,y[ ?
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP
5. Write a program that reads five real numbers and calculate their sum and their average ?