0% found this document useful (0 votes)
15 views17 pages

3 For Loop, Sttipaza42

The document is a course outline for 'Computer Science 1' at Morsli Abdellah University Center, focusing on the fundamentals of programming in Python. It covers topics such as comments, line breaks, and control structures, particularly loops, including 'for' and 'while' loops, along with their syntax and use cases. Additionally, it provides exercises for practical application of the concepts discussed.

Uploaded by

wahab.ayoub1234
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)
15 views17 pages

3 For Loop, Sttipaza42

The document is a course outline for 'Computer Science 1' at Morsli Abdellah University Center, focusing on the fundamentals of programming in Python. It covers topics such as comments, line breaks, and control structures, particularly loops, including 'for' and 'while' loops, along with their syntax and use cases. Additionally, it provides exercises for practical application of the concepts discussed.

Uploaded by

wahab.ayoub1234
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/ 17

People's Democratic Republic of Algeria

Ministry of Higher Education and Scientific Research


Morsli Abdellah University Center in Tipaza
Institute of Sciences

Course: Computer Science 1

‫اإلعالم اآللي‬
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.

Question: how to print numbers from1 to 100 ?

Use a loop which is one instruction


100 lines of code (not
that tells the program to print
practical) !!
numbers starting from 1 until 100.
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES

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:

➢for: The keyword that signals the beginning of a "for" loop.


➢variable: This is a variable that you define, and it represents each
item in the iterable as the loop iterates. You can use any variable
name you like.
➢in: The keyword used to specify the iterable you want to loop through.
➢iterable: The collection or sequence you want to iterate through. It
can be a list, tuple, string, range, or any other iterable object.
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP
In Python, the key word range is used to create a sequence of numbers. It's commonly used in
"for" loops to iterate a specific number of times.

range has the following syntax: Example 1:

range(start, stop, step)

range(start, stop)

range(stop)

• start (optional): The starting value (inclusive). Defaults to 0.


• stop (required): The stopping value (exclusive). The sequence stops before reaching this value.
• step (optional): The step or increment value between each number in the sequence. Defaults to 1.
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP

Repeat this block of code 3 times:


i =0
i =1
i =2
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES
Examples 2 : => FOR LOOP

In this example, `number` is the variable that


represents each item in the `numbers` list, and
the loop iterates through the list, printing each
number.

Print "Hello" four times in Python using a


"for" loop,
The function “range” will be explicated in
the next slide

This loop iterates through the


characters in the string "Hello" and
prints each character.
CHAPTER 2: FUNDAMENTALS OF PROGRAMMING
=> CONTROL STRUCTURES
=> ITERATIVE CONTROL STRUCTURES => FOR LOOP

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 ?

You might also like